When to Use this.getField() or getField() in Acrobat JavaScript
Question details
Understand when to use this.getField() instead of getField() in Acrobat JavaScript.

- Product
- Adobe Acrobat JavaScript
- Device & OS
- not provided
- Scenario
- Writing PDF form scripts at the document, field, page, or folder level.
- Observed behavior
- The script should retrieve a named field from the intended PDF document using a document reference appropriate to its execution context.
Identify where the script runs and confirm the exact field name in Acrobat's Prepare a form tool. Check what this refers to in that context before using it as a document reference.
Use this.getField() in a document context
Use an explicit document reference when a document-level script needs to access a form field.
In document-level scripts, this normally refers to the current document, so this.getField("FieldName") makes the target explicit. An unqualified getField("FieldName") is commonly resolved against the current document in document, field, and page scripts, but its meaning depends on the execution context.
In Acrobat, open the PDF and use All tools > Use JavaScript > Document JavaScripts to create or edit a document-level script.
In the script editor, enter this.getField("FieldName"), replacing FieldName with the exact name shown in Prepare a form.
Save the script and test it in Acrobat. Check that the returned field is not null before reading or changing its properties.

Pass a document reference to a folder-level function
Use this approach when a shared folder-level script must work with a specific PDF.
Work with PDF and Office files in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with PDF and Office files. Its format compatibility makes moving everyday documents easier; use Acrobat to develop or test Acrobat-specific JavaScript.

Frequently Asked Questions
Are this.getField() and getField() always equivalent?
No. They may resolve to the same document in common document, field, and page script contexts, but this.getField() works only when this refers to the intended document.
How do I get the document from a field event?
When event.target is the field, use event.target.doc to access its document, then call event.target.doc.getField("FieldName").
Why does getField() return null?
The named field may not exist in the document being searched. Check the exact field name in Acrobat's Prepare a form tool and verify that your document reference points to the intended PDF.
Can I use this.getField() in a folder-level script?
Only if this refers to the intended document at the time the code runs. A safer pattern is to pass a document object into the folder-level function and call doc.getField("FieldName").




