Other PDF Issues

How to Set PDF Layer Visibility on Open Using JavaScript in Adobe Acrobat

Adam DavisAdam Davis Nov 24, 2026 870 views

Question details

The user wants to automatically restore or set PDF layer visibility states when a document is opened using JavaScript.

How to Set PDF Layer Visibility on Open Using Adobe Acrobat JavaScript
Product
Adobe Acrobat
Device & OS
not provided
Scenario
Setting up a PDF document to dynamically adjust layer visibility based on form element states upon initialization.
Observed behavior
Currently looking for the correct JavaScript method to trigger visibility changes automatically on file open.
Before you start

Ensure you have Adobe Acrobat Pro installed, as the free Adobe Acrobat Reader does not allow you to author or edit Document-Level JavaScripts.

Solution 1Recommended

Use a Document-Level JavaScript to Set Layer Visibility

Create a script that executes automatically when the PDF is opened to read form values and set layer states.

Document-level scripts in Adobe Acrobat execute as soon as the PDF document initializes. By defining your logic here, you can evaluate the saved values of form fields (like checkboxes or radio buttons) and use the getOCGs() method to control the Optional Content Groups (layers) visibility.

1
Open the JavaScript tool

Open your PDF file in Adobe Acrobat Pro. Navigate to 'Tools' in the top menu, search for 'JavaScript', and select 'Document JavaScripts'.

2
Add a new script

Enter a descriptive script name (e.g., 'InitLayers') in the Script Name field and click the 'Add' button.

3
Write the visibility logic

Delete the default generated function wrapper in the editor. Write your code to retrieve layers using 'var layers = this.getOCGs();'.

4
Evaluate form fields and set states

Add logic to check your form field values (e.g., 'var val = this.getField("MyRadioButton").value;') and loop through the 'layers' array to set 'layers[i].state = true' or 'false' depending on the condition.

5
Save and test

Click 'OK' to close the JavaScript editor. Save the document, close it, and reopen it to verify that the layer visibility updates automatically.

Use a Document-Level JavaScript to Set Layer Visibility
Understanding OCGs: The Acrobat JavaScript API uses Optional Content Groups (OCGs) to represent layers. You must iterate through the OCG array and check the 'name' property to find and toggle specific layers.
Free Microsoft Office alternative

Looking for a Lightweight, All-in-One PDF and Office Solution?

While Adobe Acrobat is highly specialized for advanced JavaScript layer controls, WPS Office provides a seamless, lightweight, and free alternative for everyday PDF viewing, editing, and Office document management. Easily handle PDFs, Word, Excel, and PowerPoint files in one familiar tabbed interface.

  1. 1. Download the installer: Visit the official WPS website and download the free installer for your operating system.
  2. 2. Install WPS Office: Run the setup file and follow the on-screen instructions to install the suite.
  3. 3. Manage your documents: Launch WPS Office to easily open, edit, and manage your PDFs and Microsoft Office documents in one place.
Free, lightweight, and fast all-in-one office suiteFully compatible with Microsoft Office formats (DOCX, XLSX, PPTX)Comprehensive PDF tools for reading, annotating, and convertingFamiliar user interface for seamless and quick migration
microsoft office alternative - wps office

Frequently Asked Questions

Why doesn't my JavaScript run when I open the PDF in a web browser?

Most built-in web browser PDF viewers (like Chrome, Edge, or Safari) do not support Acrobat JavaScript. The PDF must be opened in a dedicated desktop application like Adobe Acrobat or Adobe Reader for document-level scripts to execute properly.

Can I trigger layer visibility changes on a button click instead of on open?

Yes. You can add a 'Run a JavaScript' action to a button's 'Mouse Up' event in Acrobat. This allows users to toggle layer visibility dynamically while interacting with the document.

How do I find the exact name of the layer in Acrobat JavaScript?

You can use 'this.getOCGs()' to retrieve an array of all layers in the document. By looping through this array and logging or checking the 'name' property of each OCG object, you can identify and target the specific layer you want to modify.