Check Whether an Acrobat PDF Field Exists with JavaScript
Question details
Check whether an optional PDF form field exists before changing its display property with Acrobat JavaScript.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- A PDF form may have an electrical icon, a water icon, or both, depending on the building.
- Observed behavior
- Attempting to change the display property of a field that does not exist causes a JavaScript error.
Open the PDF in Adobe Acrobat and check the exact names of the optional fields in the form editor. Make sure the variable passed to getField contains the intended field name.
Check the getField result before hiding an optional field
Use a conditional check to hide a field only when it exists in the PDF.
Acrobat's getField method returns null when it cannot find the named field. The error occurs if the script then tries to access a property such as display on that null result.
In Acrobat, open the PDF's form editor and locate the JavaScript action that changes the icon field's visibility.
Replace the direct property assignment with: var field = this.getField(buildingName); if (field) { field.display = display.hidden; } Here, buildingName must contain the exact field name, and this must refer to the PDF document.
Repeat the getField and if check for each optional electrical or water icon field, using its corresponding field name.
Preview the form and trigger the action for buildings with different combinations of icon fields. Existing fields should be hidden, and missing fields should be skipped.

Work with PDFs and Office files in WPS Office
WPS Office is a free, lightweight alternative for everyday document work. Its familiar interface and support for common Office formats make it easy to move between files, while its PDF tools let you open and view PDFs. Use Acrobat to edit and test Acrobat-specific JavaScript.

Frequently Asked Questions
What does getField return when a PDF field is missing?
Acrobat JavaScript returns null. Store the result in a variable and check it with if (field) before accessing field properties.
Why do I get an error when setting a missing field's display property?
A missing field produces a null result, and null has no display property. Check the getField result before assigning display.hidden.
How can I check more than one optional icon field?
Call this.getField with each icon's exact field name and use a separate if check before changing each field's display property.
What should I check if an existing field is not hidden?
Verify its exact name in Acrobat's form editor, confirm that the variable passed to getField contains that name, and check that this refers to the PDF document.




