1

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...

Community
  • 1
  • 1
TheIronCheek
  • 1,077
  • 2
  • 20
  • 50

2 Answers2

1

Why use while… for this?

Doing exactly the same (setting the mousetip text to a blank string) is simpler using

for (var i = 0 ; i < this.numFields ; i++) {
this.getField(this.getNthFieldName(i)).userName = "" ;
}

and that should do it.

However, unless you have a very good reason, setting the userName to the blank string is not recommended; it is needed if your form is used with assistive devices, and it is also the very nearest and simplest help item.

Max Wyss
  • 3,549
  • 2
  • 20
  • 26
  • I'm getting essentially the same error: `TypeError: this.getField(this.getNthFieldName(i)) is null`. – TheIronCheek Mar 01 '16 at 13:35
  • My issue isn't a while vs for loop problem. It's something else. – TheIronCheek Mar 01 '16 at 13:36
  • That sounds a lot like a botched up PDF document. The loop sees a fieldname, but the according field does not exist. You may try to locate the culprit using either an app.alert() of the field name before the script touches the field, or write the field name to the Console (or use the debugger and set breakpoints). How was the form created? Does a Save as… in Acrobat cure the issue (just make sure that you have deactivated the "save as… optimizes for fast web view" option in the Preferences). – Max Wyss Mar 01 '16 at 14:41
  • I already tried outputting all the names to the console and it worked fine. it just doesn't like the `this.getField(nom)` line. The form was created using the automatic form recognition feature where it attempts to find all your fields based on what it perceives as a blank. – TheIronCheek Mar 01 '16 at 14:54
  • Ugh... figured it out. Apparently the auto field generator named some fields with a leading space which caused the script to error. I fixed the half dozen fields and now it works like a charm. – TheIronCheek Mar 01 '16 at 14:56
  • Glad to hear that it works now. And another reason to not use the automatic field creation, but do some analyzing and planning the form, and add the fields manually; my experience shows that fixing the automatically created fields takes more time than doing it right from the beginning… – Max Wyss Mar 01 '16 at 17:45
1

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.

TheIronCheek
  • 1,077
  • 2
  • 20
  • 50