How to Dynamically Insert and Hide PDF Page Templates in Adobe Acrobat
Question details
The user needs to dynamically insert a template page right after a specific page when a checkbox is selected, and automatically delete it when the checkbox is unchecked.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Designing an interactive PDF form where pages dynamically appear or disappear based on user checkbox selections.
- Observed behavior
- Instead of unhiding a template at the very end of the document, the user wants the template to spawn directly after the active page, and requires a script to securely remove the page upon deselection.
Ensure you have Adobe Acrobat Pro installed and your interactive form's template pages are correctly configured in the Page Templates menu, as this process requires writing advanced custom JavaScript actions.
Use JavaScript to Spawn and Delete Template Pages
Apply a custom JavaScript action to the form's checkbox to handle both inserting the template at a specific page index and deleting it when deselected.
When you simply unhide a template page, Adobe Acrobat automatically places it at the very end of the file. To insert the template immediately after the current page, you need to use the Acrobat JavaScript `Template.spawn()` method. To reverse this action and make the page disappear, the `this.deletePages()` method is used to cleanly remove the newly spawned page.
Open your interactive PDF document in Adobe Acrobat and navigate to the 'Prepare Form' tool from the right-hand pane.
Right-click the checkbox you want to use as a trigger for the dynamic page and select 'Properties'.
Navigate to the 'Actions' tab. Select 'Mouse Up' as the trigger, choose 'Run a JavaScript' from the Select Action dropdown, and click 'Add'.
Enter the script logic: `if(event.target.value != 'Off') { this.getTemplate('MyTemplate').spawn(event.target.page + 1, true, false); } else { this.deletePages(event.target.page + 1); }`. Replace 'MyTemplate' with the exact name of your hidden page template.
Click 'OK' to save the script, close the properties window, exit form editing mode, and test the checkbox to verify the page is dynamically inserted and deleted.

Unhide and Move the Page (Alternative Method)
If you prefer unhiding over spawning, use the movePage command to reposition the unhidden template from the end of the document to your desired location.
Easily Manage PDF Pages and Forms with WPS Office
While scripting dynamic form actions requires advanced coding in Adobe Acrobat, WPS Office provides an intuitive, highly visual PDF editor. You can easily insert, extract, delete, and organize PDF pages manually, as well as fill out interactive forms without writing complex JavaScript code.
- 1. Open your PDF document: Launch WPS Office and open your existing PDF file or interactive form.
- 2. Navigate to the Page tab: Click on the 'Page' tab on the top ribbon to access visual page management tools.
- 3. Insert new pages: Click 'Insert Page' to easily add a blank page or import a page from another PDF document exactly where you need it.
- 4. Delete unwanted pages: To remove unnecessary pages quickly, simply select the target page thumbnail and click the 'Delete' button.

Frequently Asked Questions
Why does my unhidden PDF template always appear at the end of the document?
By default, Adobe Acrobat appends newly unhidden template pages to the very end of the PDF file. To place them exactly where you want, you must either use the `movePage` command after unhiding or use the `Template.spawn()` method to insert them dynamically at a specific index.
Can I use dynamic template spawning in Adobe Reader?
Dynamic page spawning typically requires Adobe Acrobat Pro to author and set up. However, users viewing the document with the free Adobe Reader can trigger the spawned pages only if the original creator saved the document with 'Reader Enable Usage Rights' applied.
How do I remove a spawned page using a checkbox?
You can assign a JavaScript action to the checkbox's 'Mouse Up' event that detects if the checkbox is unchecked (e.g., ensuring its value is 'Off'). Once detected, the script should run `this.deletePages(event.target.page + 1)` to specifically remove the spawned page.
Does using deletePages affect the original hidden template?
No. The `this.deletePages()` command only removes the visible, active page at the specified index that was created. Your original hidden template remains safely stored in the document's background data, ready to be spawned again.




