How to Turn On All PDF Layers Using Acrobat JavaScript
Question details
The user needs to programmatically activate all optional content groups (layers) within a PDF document using Acrobat JavaScript, ignoring the default states assigned in Illustrator or Acrobat.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Managing complex PDFs with multiple layers where making every layer visible simultaneously is required without manually toggling each one.
- Observed behavior
- Layers are turned off by default based on initial saved states, and need to be forced to a visible state in the current view using a script.
Ensure you have access to Adobe Acrobat Pro to use the JavaScript Debugger console, and remember that scripts modifying the current view state may not permanently alter the saved file.
Use the Acrobat JavaScript Console to Toggle All Layers
Execute a brief JavaScript loop to fetch all Optional Content Groups (OCGs) and instantly change their current state to visible.
Instead of trying to detect specific layer names or evaluating which layers are currently hidden, you can force all layers to turn on universally.
Changing the state of a layer through this specific script does not mark the document as modified. Consequently, the layers will revert to their initial default state when the document is reloaded.
Launch Adobe Acrobat Pro, open your PDF document, and navigate to 'Tools' > 'JavaScript'. Click on 'Document JavaScripts' or press Ctrl+J (Windows) / Cmd+J (Mac) to open the JavaScript Debugger console.
In the console window, type or paste the following code: var ocgsArr = this.getOCGs(); for (var i in ocgsArr) { ocgsArr[i].state = true; }
Highlight all the text you just pasted into the console, then press the Enter key on your numeric keypad (or Ctrl+Enter) to run the script. All layers in your document will instantly become visible.

Easily Manage PDF Layers Visually with WPS Office
If you want to avoid writing JavaScript code to manage your document's visibility, WPS Office provides an intuitive visual interface to effortlessly toggle and manage PDF layers.
- 1. Open your PDF in WPS Office: Launch WPS Office and open your multi-layered PDF document by clicking 'Open' on the home screen.
- 2. Access the Layers Panel: Navigate to the left-hand navigation pane and click on the 'Layers' icon (it looks like stacked sheets of paper) to reveal all Optional Content Groups.
- 3. Toggle layer visibility: Click the 'Eye' icon next to any layer's name to instantly show or hide that specific layer within your document.

Frequently Asked Questions
Why do my PDF layers hide again after reopening the file?
When you use the JavaScript command `ocgsArr[i].state = true`, you are only modifying the current session's view state. To make it permanent, you would need to modify the layer's initial state (`initState`) and save the document.
What is an Optional Content Group (OCG)?
An Optional Content Group (OCG) is Adobe Acrobat's technical term for a PDF layer. It allows document authors to control the visibility of specific content elements—such as multiple languages or alternative graphics—within a single PDF file.
Can I embed this JavaScript so users can click a button to show layers?
Yes. You can attach this JavaScript snippet to a custom PDF Button or a Bookmark. When the user clicks the button or bookmark, the script will execute and reveal all layers immediately.
Does this script work in free PDF readers?
Running custom JavaScript through the console typically requires advanced PDF software like Adobe Acrobat Pro. However, buttons containing this script can often be clicked and executed in standard free readers, provided they support basic JavaScript interactions.




