Set multiple fields to certain format

Oct 17, 2017

Copy link to clipboard

Copied

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?

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);

TOPICS
Acrobat SDK and JavaScript, Windows

Views

324

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Set multiple fields to certain format

Oct 17, 2017

Copy link to clipboard

Copied

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?

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);

TOPICS
Acrobat SDK and JavaScript, Windows

Views

325

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 17, 2017 0
Oct 17, 2017

Copy link to clipboard

Copied

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);

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 17, 2017 0
Oct 17, 2017

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 17, 2017 1
Oct 18, 2017

Copy link to clipboard

Copied

Thanks, that works great!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 18, 2017 0