How to Spawn and Delete PDF Pages Using JavaScript
Question details
The user needs to execute a JavaScript command via a button in a PDF form that spawns a new page from a template and instantly deletes the currently active page.

- Product
- Device & OS
- not provided
- Scenario
- Creating dynamic PDF forms where clicking a button replaces the current page with a new templated page.
- Observed behavior
- Using generic page numbering properties for deletion causes the wrong page to be deleted because the page indexes shift dynamically after a new page is spawned.
Ensure you are testing your JavaScript actions in a fully-featured desktop PDF editor, as most built-in browser PDF viewers do not support advanced JavaScript commands like template spawning or page deletion.
Use Event Target Property for Sequential Spawn and Delete
By referencing the triggering button's specific page property, you can ensure the correct original page is deleted even after the new page alters the document's total page index.
When spawning a new page in a PDF, the index of all subsequent pages changes. If your script relies on the generic document pageNum property to delete a page, it may accidentally delete the newly spawned page or an unrelated page. The safest approach is to capture the page where the button resides before the index shifts.
Right-click the button in your PDF form and select 'Properties' to access the form field configurations.
Navigate to the 'Actions' tab, select 'Mouse Up' as the trigger, choose 'Run a JavaScript' from the action drop-down menu, and click 'Add'.
In the JavaScript editor, begin by writing the command to spawn your hidden template. For example: var newPage = this.getTemplate('TemplateName').spawn();
Instead of using the pageNum variable, capture the current page via the button event and delete it. Add this to your script: var currentPage = event.target.page; this.deletePages(currentPage, currentPage);

Manage PDF Pages and Forms Easily with WPS Office
If you prefer not to write complex JavaScript for basic page manipulation, WPS PDF offers an intuitive visual interface to insert, extract, replace, and delete PDF pages seamlessly while fully supporting standard PDF form interactions.
- 1. Open Your Document: Launch WPS Office and open your target PDF file using the WPS PDF tool.
- 2. Access the Page Manager: Click on the 'Page' tab located in the top navigation ribbon to open the page management interface.
- 3. Insert New Pages: Click 'Insert' to quickly add blank pages or import pages directly from another PDF document or template file.
- 4. Delete Unwanted Pages: Select the thumbnail of the page you wish to remove and click the 'Delete' button to instantly remove it from the document.

Frequently Asked Questions
Why does my PDF JavaScript fail to run in Chrome or Edge?
Most built-in web browser PDF viewers only support basic document rendering and standard form filling. They lack the API support for advanced Acrobat JavaScript methods such as `spawn()` or `deletePages()`. You must use a dedicated desktop PDF application.
Can I spawn multiple pages from a template at once?
Yes, you can use a JavaScript loop to generate multiple pages from a single template. Ensure you properly handle the naming and index tracking within your loop to avoid runtime conflicts.
Why is the wrong page being deleted when I use pageNum?
When a new page is spawned and inserted before your current position, the total page count increases and all subsequent page indexes shift dynamically. The `pageNum` variable may then point to a completely different page than it originally did. Referencing `event.target.page` solves this issue.




