PDF Editing Issues

How to Show a PDF Layer Based on Checkbox and Text Field in Acrobat

Tauseeq MagsiTauseeq Magsi Oct 28, 2026 869 views

Question details

The user wants to display specific optional content groups (layers) in a PDF depending on whether a checkbox is selected and if a text field meets a specific numerical condition.

How to Show an Acrobat PDF Layer Based on a Checkbox and Text Field
Product
Adobe Acrobat
Device & OS
not provided
Scenario
Creating an interactive PDF form where layer visibility dynamically changes based on user input from multiple form fields.
Observed behavior
The JavaScript logic fails to toggle the correct layers because of incorrect array indexing and retrieving Optional Content Groups (OCGs) improperly without looping.
Before you start

Ensure you have access to Adobe Acrobat Pro to edit form fields and run JavaScript, and verify the exact names of your layers (Optional Content Groups), checkboxes, and text fields.

Solution 1Recommended

Correcting the JavaScript Logic for PDF Layers

Use proper array indexing to loop through OCGs and apply conditional statements based on the checkbox state and text field value.

When toggling layers using Adobe Acrobat JavaScript, this.getOCGs() returns an array of layer objects. You must use ocgArray[i].name and ocgArray[i].state within your loop rather than applying properties directly to the array variable.

To check layers across specific pages, concatenate the arrays from each page, such as using this.getOCGs(0).concat(this.getOCGs(1)).

1
Access Field Properties

Select the Checkbox field in your PDF form, right-click it, and choose 'Properties'.

2
Add a JavaScript Action

Navigate to the 'Actions' tab, select 'Mouse Up' as the trigger, choose 'Run a JavaScript' from the dropdown, and click 'Add'.

3
Retrieve Field Values

In the JavaScript editor, retrieve the text field value using this.getField('PIED').value and convert it to a number for comparison.

4
Loop Through OCGs

Initialize a loop to iterate through the layers using: var ocgArray = this.getOCGs(); for (var i=0; i < ocgArray.length; i++) { ... }

5
Apply Conditional Logic

Inside the loop, use conditional statements like if (ocgArray[i].name == 'PA4') { ocgArray[i].state = true; } to toggle visibility based on your checkbox and text field rules.

Correcting the JavaScript Logic for PDF Layers
Syntax Check: Always ensure you are targeting ocgArray[i].name rather than ocgArray.name. The latter will fail because the array itself does not have a name property.
WPS PDF Advanced Features

Easily Create Interactive PDF Forms with WPS Office

While complex layer scripting requires advanced Acrobat JavaScript, you can easily design, manage, and fill interactive PDF forms—including text fields and checkboxes—using WPS PDF.

  1. 1. Open Your PDF: Launch WPS Office and open your existing PDF document.
  2. 2. Navigate to the Form Tab: Click on the 'Form' tab located on the top ribbon to access interactive elements.
  3. 3. Insert Form Fields: Select 'Check Box' or 'Text Field' and click on the page to insert the interactive element.
  4. 4. Customize Field Properties: Double-click the inserted field to open the Properties menu, where you can define its name, appearance, and options.
  5. 5. Save the Document: Save your PDF to lock in the interactive form fields for your users.
Create and edit PDF form fields effortlesslyHighly compatible with standard PDF formatsLightweight, fast, and user-friendly interfaceCross-platform support for Windows, Mac, and mobile
microsoft office alternative - wps office

Frequently Asked Questions

Why doesn't ocgArray.name work in my Acrobat JavaScript?

ocgArray is an array object, which does not possess a 'name' property natively. You must loop through the array and access the name of each individual Optional Content Group (layer) using ocgArray[i].name.

How do I get layers from multiple pages in Adobe Acrobat?

You can retrieve and combine layers from specific pages by using the concat() method. For example, executing this.getOCGs(0).concat(this.getOCGs(1)) will return an array of layers from both the first and second pages.

How do I convert a text field value to a number for comparison in Acrobat JS?

You can retrieve the field value using this.getField('FieldName').value and wrap it with Number() or parseInt() to ensure it evaluates correctly as a numeric value when checking conditions like >= 18.

Can I show or hide a text field instead of a layer based on a checkbox?

Yes, instead of targeting OCGs (layers), you can target the text field directly using this.getField('TextFieldName').display and set it to display.visible or display.hidden based on your checkbox condition.