How to Toggle PDF Layers On and Off with a Button in Adobe Acrobat
Question details
The user wants to create an interactive button within a PDF that can repeatedly show and hide a specific layer when clicked.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Creating an interactive document, such as a building diagram, where different parts or optional content groups need to be dynamically displayed or hidden by the viewer.
- Observed behavior
- The user needs a reliable method to switch the visibility state of a target layer on and off without permanently altering the file's saved default state.
Ensure your PDF document already contains the necessary layers (Optional Content Groups) and that you have access to Adobe Acrobat Pro to add form buttons and edit actions.
Use JavaScript to Toggle the Layer State
Add a custom JavaScript action to a single form button to dynamically switch the visibility of the target layer on and off.
This method uses a small JavaScript snippet to identify the target layer by its name and flip its current visibility state. It provides a clean, single-button interactive experience.
Open your document in Adobe Acrobat Pro. Go to the 'Prepare Form' tool, select the Button icon, and place a new button on your PDF page.
Double-click the newly created button to open the Button Properties dialog box. Navigate to the 'Actions' tab.
Set the 'Select Trigger' dropdown to 'Mouse Up'. In the 'Select Action' dropdown, choose 'Run a JavaScript', then click the 'Add' button.
In the JavaScript editor, paste the following code (replace 'Delta Inv' with your actual layer name): var ocgs = this.getOCGs(); for (var i in ocgs) { if (ocgs[i].name == 'Delta Inv') { ocgs[i].state = !ocgs[i].state; break; } }.
Click 'OK' to close the editor, then 'Close' on the properties window. Exit form editing mode and click your button to verify that the layer turns on and off.

Use Overlapping Buttons with Native Show/Hide Actions
A code-free alternative that uses two overlapping buttons with Acrobat's built-in layer visibility actions to simulate a toggle effect.
View and Manage Interactive PDFs with WPS Office
WPS Office provides a comprehensive, lightweight PDF toolset that allows you to easily view layered documents, interact with form buttons, and manage PDF content without needing expensive software.
- 1. Open Your PDF: Launch WPS Office, select the PDF tab, and open your layered or interactive PDF document.
- 2. Use Interactive Buttons: Navigate through your document in viewing mode to seamlessly click and trigger standard form buttons embedded in the file.
- 3. Manage Layers Manually: If you need to view layers without using buttons, open the left navigation pane and click the 'Layers' icon to manually toggle optional content groups on and off.

Frequently Asked Questions
Why does the PDF layer revert to its original state when I reopen the file?
By design, interactive layer visibility changes triggered by JavaScript or form buttons are temporary and meant for the current viewing session. They do not alter the saved default state of the document, ensuring the file always opens in its baseline configuration.
Can I toggle PDF layers without writing any JavaScript?
Yes, you can use Acrobat's built-in actions. By creating two overlapping buttons, you can set one button's action to show the layer and hide itself, while the other hides the layer and shows the first button, creating a seamless toggle effect.
Will my JavaScript toggle button work in all PDF viewers?
Not necessarily. Custom JavaScript actions for Optional Content Groups (OCGs) are primarily supported by robust desktop PDF software like Adobe Acrobat and Adobe Reader. Some third-party, mobile, or web-based PDF readers may restrict or ignore these scripts.




