How to Change a PDF Layer's Default State Using Acrobat JavaScript
Question details
The user needs to save a PDF layer's current visibility state dynamically so that it remains persistently visible or hidden the next time the document is opened.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form where specific layers (Optional Content Groups) must retain their toggled on/off status after the user saves and closes the file.
- Observed behavior
- By default, PDF layers revert to their initial default visibility state when reopened, discarding any visibility changes the user triggered via buttons during their session.
Ensure you are editing your form in a full PDF editor like Adobe Acrobat Pro. Standard Adobe Reader often lacks the necessary permissions to persistently save layer states directly through JavaScript without workarounds.
Store Layer State in a Hidden Text Field
Use a hidden text field to record the layer's state and apply a document-level JavaScript to restore it upon opening.
Because directly modifying the default state of a layer via script doesn't always persist when saving (especially if opened later in Adobe Reader), the most reliable method is to save the state as a value inside a hidden form field. When the document opens, a separate script reads this field and toggles the layer accordingly.
Add a new text field to your PDF form. Open its Properties, navigate to the General tab, and set its form field visibility to 'Hidden'. Name the field something identifiable, like 'LayerState'.
Add a JavaScript action to the button or checkbox that toggles your layer. Use the this.getOCGs() method to find your specific layer, and update your hidden 'LayerState' field with a value of '1' (visible) or '0' (hidden).
Go to Document JavaScripts (or the Document 'Will Save' / 'Page Open' event) and add a script that runs when the PDF loads. Write a script that reads the value of 'LayerState' and sets the layer's .state property to match the saved value.

Manage Your PDFs and Office Documents Seamlessly with WPS Office
If you are looking for an easier way to handle documents without relying on complex JavaScript coding, WPS Office offers a lightweight, all-in-one suite. It seamlessly manages Word, Excel, PowerPoint, and PDF files natively, serving as an excellent alternative to Microsoft Office and standalone PDF readers.
- 1. Download the software: Visit the official WPS Office website and click the free download button for your operating system.
- 2. Install the application: Run the setup file and follow the quick on-screen instructions to install the suite.
- 3. Open your files: Launch WPS Office to immediately open, view, and edit your PDFs and Office documents in a single, tabbed interface.

Frequently Asked Questions
Why don't PDF layers save their state automatically?
PDF layers, also known as Optional Content Groups (OCGs), load their default visibility state exactly as defined in the document properties during creation. Changes made dynamically by a user interacting with the form during a session are temporary unless a script specifically writes the state to a persistent form field.
Can I change the layer visibility state without using JavaScript?
Yes, you can manually change the default visibility of a layer in Adobe Acrobat without scripting. Open the Layers panel on the left, right-click the specific layer, select Properties, and change the 'Default State' to On or Off. However, dynamic user-driven changes still require JavaScript.
How does the hidden text field workaround operate?
The hidden text field acts as a persistent storage variable. When a user clicks a button to toggle a layer, a script updates this text field. Because form field data is saved when a user saves the PDF, a separate script can read this text field the next time the document is opened to re-apply the correct layer visibility.




