Search code examples
javascriptpdfnulladobeacrobat

Adobe Acrobat - Error Iterating Over All Fields in a PDF with JavaScript


I'm having trouble iterating over all of the fields in my document to remove the tooltip. Here's my code:

var index=0;
while(index<this.numFields)
{
    var nom=this.getNthFieldName(index);
    var fieldName=this.getField(nom);
    fieldName.userName = "";
    index=index+1;
}

I'm getting an error saying fieldName is null and my script won't run. I've seen this answer already:

Iterating over all fields in a PDF form with JavaScript

I get the same error with that code too. If I manually assign a field name to fieldName using var fieldName=this.getField("field1");, it works fine.

Does anyone have any idea why this would error on me?

Edit:

I can iterate over the list and output nom to the console so I know it's grabbing the names of the fields properly. It seems to have trouble dropping that name into the this.getField(nom) statement. No idea why...


Solution

  • I figured out my issue.

    When I created the form, I used the automatic field detection to create my fields for me in order to save time (there are like 250 fields on this form). The reason I needed the script in the first place was to remove the crummy tooltip names that the feature generates.

    Apparently, in its infinite wisdom, the field detection feature named a handful of fields with a leading space ( something like " OF INFORMATIONrow1"). Since getNthFieldName(index) returns the fields in alphabetical order, it was returning one of these broken fields and erroring immediately because getField() doesn't like the leading space in the name.

    I renamed the handful of fields and the script works like a charm.