Others

Check Whether an Acrobat PDF Field Exists with JavaScript

Bushra ParveenBushra Parveen Nov 18, 2026 869 views

Question details

Check whether an optional PDF form field exists before changing its display property with Acrobat JavaScript.

Check Whether an Acrobat PDF Field Exists with 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.
Before you start

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.

Solution 1Recommended

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.

1
Open the form script

In Acrobat, open the PDF's form editor and locate the JavaScript action that changes the icon field's visibility.

2
Check the field before hiding it

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.

3
Check each optional field

Repeat the getField and if check for each optional electrical or water icon field, using its corresponding field name.

4
Test the form

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.

Check the getField result before hiding an optional field
Check field names: A misspelled field name also returns null, so verify the name in the form editor if an existing icon is not hidden.
Free Microsoft Office alternative

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.

Open and view PDF documents alongside Office files.Compatible with common Microsoft Office formats, including DOCX, XLSX, and PPTX.Use a familiar interface for a smoother transition from Microsoft Office.Get a free, lightweight suite for everyday document tasks.
microsoft office alternative - wps office

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.