Skip to main content
bcs marketing
Known Participant
October 17, 2017
Answered

Set multiple fields to certain format

  • October 17, 2017
  • 1 reply
  • 1144 views

I have a PDF form that's gone awry; some check boxes turn black when printing, while others don't and there's no clear reason for this. Worse is that those black check boxes are completely black regardless of whether they're checked or not, so there's no telling whether they're actually checked.

I've saved the scripts from the "All JavaScripts" option of the form, as the scripts work great, and now I want to load those into a new export (from InDesign) of the form. Unfortunately that doesn't work; none of the scripts seem to save in the new form. I'm guessing it's because they can't be bound to the proper fields due to them not having the correct formatting set yet.

All fields are named logically as in "quantity-[product]" and "net-price-[product]", so I'm thinking it should be possible to run a JavaScript once (using an action set to a button press?) to set all fields whose names start with a certain string ("net-price" for example) to a certain format. Now that brings me to my problem: I can't get the sample scripts found online to work at all.

var theField = getField(getNthFieldName(fieldNumber));

for (i = 0; i < numFields; i++) {

  if (theField.value.indexOf("net-price-") = "0") {

    theField.setAction("Format", 'AFNumber_Format(2, 2, 0, 0, "\u20ac", false)');

  }

}

Can anyone offer me some advice on this?

This topic has been closed for replies.
Correct answer try67

Using code you found online is often risky... The code above contains multiple errors in it.

What you want to do is possible, though. Assuming the format settings in the code above are correct, try this code:

var counter = 0;

for (var i=0; i<this.numFields; i++) {

    var f = this.getField(this.getNthFieldName(i));

    if (f==null) continue;

    if (/^quantity-/.test(f.name) || /^net-price-/.test(f.name)) {

        f.setAction("Format", 'AFNumber_Format(2, 2, 0, 0, "\u20ac", false)');

        counter++;

    }

}

app.alert("Done. " + counter + " fields were edited.",3);

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
October 17, 2017

Using code you found online is often risky... The code above contains multiple errors in it.

What you want to do is possible, though. Assuming the format settings in the code above are correct, try this code:

var counter = 0;

for (var i=0; i<this.numFields; i++) {

    var f = this.getField(this.getNthFieldName(i));

    if (f==null) continue;

    if (/^quantity-/.test(f.name) || /^net-price-/.test(f.name)) {

        f.setAction("Format", 'AFNumber_Format(2, 2, 0, 0, "\u20ac", false)');

        counter++;

    }

}

app.alert("Done. " + counter + " fields were edited.",3);

try67
Community Expert
Community Expert
October 17, 2017

PS. I've developed a tool that allows you to do it easily and quickly, without having to write any code:

Custom-made Adobe Scripts: Acrobat -- Apply Format to Multiple Text Fields