PDF Editing Issues

How to Show or Hide PDF Pages Dynamically with JavaScript

Chanuka GeekiyanageChanuka Geekiyanage Oct 28, 2026 869 views

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.

How to Show or Hide PDF Pages Dynamically with 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 start

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.

Solution 1Recommended

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.

1
Create a Page Template

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.

2
Add a Trigger Element

Insert a checkbox or button into your main PDF form. Right-click the element, go to 'Properties', and navigate to the 'Actions' tab.

3
Write the JavaScript Code

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.

Use JavaScript Template Spawning to Show or Hide Pages
Adobe Reader Limitations: Spawning and deleting pages via JavaScript often requires elevated privileges. While it works flawlessly in Acrobat Pro, Adobe Reader users may experience restricted functionality unless the PDF has specific extended rights enabled.
WPS PDF Editor

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. 1. Open your PDF: Launch WPS Office and open your PDF document using the WPS PDF tool.
  2. 2. Edit Pages Visually: Navigate to the 'Page' tab on the top ribbon to easily insert, delete, extract, or rearrange pages.
  3. 3. Manage Forms: Use the 'Form' tab to interact with standard fields like checkboxes, text boxes, and buttons without needing complex JavaScript.
Easily insert, delete, or extract PDF pages using a visual interface without complex coding.Create standard interactive forms compatible with mainstream PDF viewers.Seamlessly view and edit Microsoft Office and Adobe PDF formats.Lightweight, fast, and free to use for daily PDF tasks.
microsoft office alternative - wps office

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.