How to Set PDF Layer Visibility on Open Using JavaScript in Adobe Acrobat
Question details
The user wants to automatically restore or set PDF layer visibility states when a document is opened using 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.
Ensure you have Adobe Acrobat Pro installed, as the free Adobe Acrobat Reader does not allow you to author or edit Document-Level JavaScripts.
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.
Open your PDF file in Adobe Acrobat Pro. Navigate to 'Tools' in the top menu, search for 'JavaScript', and select 'Document JavaScripts'.
Enter a descriptive script name (e.g., 'InitLayers') in the Script Name field and click the 'Add' button.
Delete the default generated function wrapper in the editor. Write your code to retrieve layers using 'var layers = this.getOCGs();'.
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.
Click 'OK' to close the JavaScript editor. Save the document, close it, and reopen it to verify that the layer visibility updates automatically.

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. Download the installer: Visit the official WPS website and download the free installer for your operating system.
- 2. Install WPS Office: Run the setup file and follow the on-screen instructions to install the suite.
- 3. Manage your documents: Launch WPS Office to easily open, edit, and manage your PDFs and Microsoft Office documents in one place.

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.




