How to Show or Hide PDF Pages Dynamically with JavaScript
Question details
The user wants to dynamically show or hide a specific page in a PDF form when clicking a button or checkbox, using JavaScript.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form where pages appear or disappear based on user input.
- Observed behavior
- Needs a method to control page visibility dynamically and wants to know the script's compatibility with Adobe Reader.
Before you begin, ensure you have access to a full-featured PDF editor like Adobe Acrobat Pro, as standard PDF readers generally do not support creating or managing page templates via JavaScript.
Use JavaScript Template Spawning to Show or Hide Pages
The most reliable method to dynamically add or remove pages in a PDF form is by using hidden page templates and JavaScript.
Directly hiding a standard PDF page is not natively supported in the PDF specification. Instead, you can set up a hidden template page.
When the user interacts with a checkbox or button, JavaScript spawns a copy of this template to make it visible, and deletes the spawned page to hide it.
Design the page you want to show or hide. Go to the 'Organize Pages' or 'Page Templates' menu in your PDF editor, name your template, and set the property to be hidden initially.
Insert a checkbox or button into your main PDF form. Right-click the element, go to 'Properties', and navigate to the 'Actions' tab.
Set the action trigger to 'Run a JavaScript'. Use the getTemplate() and spawn() methods to add the page when the box is checked, and the deletePages() method to remove it when unchecked.

Manage PDF Pages and Forms Easily with WPS Office
While advanced JavaScript page spawning requires heavyweight PDF editors, WPS PDF offers a lightweight, user-friendly solution for managing pages, editing content, and filling PDF forms effortlessly.
- 1. Open your PDF: Launch WPS Office and open your PDF document using the WPS PDF tool.
- 2. Edit Pages Visually: Navigate to the 'Page' tab on the top ribbon to easily insert, delete, extract, or rearrange pages.
- 3. Manage Forms: Use the 'Form' tab to interact with standard fields like checkboxes, text boxes, and buttons without needing complex JavaScript.

Frequently Asked Questions
Can I simply hide a page using standard PDF features without JavaScript?
No, standard PDF specifications do not include a simple toggle to hide or unhide a page. Pages must either be deleted or spawned from hidden templates using JavaScript to achieve this effect.
Will template spawning JavaScript work in all PDF viewers?
Advanced JavaScript methods like spawn() or deletePages() are highly specific to Adobe Acrobat. Many third-party PDF viewers, including mobile apps and web browsers, may not support these scripts.
How do I hide form fields instead of entire pages?
If you only need to hide content rather than an entire page, you can group form fields and use JavaScript to change their display property (e.g., setting them to display.hidden or display.visible) based on a checkbox click.




