How to Spawn a PDF Template After the Current Page in Adobe Acrobat
Question details
The user needs to programmatically spawn a hidden PDF page template directly after their current active page using JavaScript.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Using JavaScript to insert a hidden template dynamically based on the user's active page index without causing page conflicts.
- Observed behavior
- The user wants to use a script utilizing dynamic variables (like this.pageNum) to insert the spawned template right after the active page without accidentally overwriting existing content.
Before running JavaScript to modify PDF pages, ensure your document has a named, hidden template page already set up and that you have a backup of your original file to prevent accidental data loss.
Use JavaScript to Spawn the Template Based on Page Number
Utilize the getTemplate() and spawn() methods, passing this.pageNum + 1 to safely insert the template immediately after the current page.
Instead of hardcoding the page index or relying on event.target.page (which can return an array if there are multiple copies of a field), you can dynamically target the location using this.pageNum.
To place the new page immediately following the active page, add 1 to the current page number. Be careful not to spawn exactly at this.pageNum unless you have configured field renaming, as it can cause overlapping.
In Adobe Acrobat, go to 'Tools' > 'JavaScript' and select 'Document JavaScripts', or add a 'Run a JavaScript' action to a form button.
Declare a variable to get your hidden template using the command: var template = this.getTemplate('YourTemplateName');
Execute the spawn command targeting the next page index: template.spawn(this.pageNum + 1, false, false);
Run the script from different pages in your PDF to ensure the template is dynamically inserted right after your current viewing position.

Spawn the Template at the End of the Document
If managing relative page indexes becomes complex or leads to overwritten content, you can spawn the template safely at the end of the PDF.
Manage PDF Pages and Templates Easily with WPS Office
For users who prefer a straightforward visual interface over writing JavaScript, WPS Office provides powerful PDF editing tools. You can easily insert, extract, and manage PDF pages or templates without any coding.
- 1. Open Your PDF in WPS Office: Launch WPS Office and open your PDF document using the built-in WPS PDF editor.
- 2. Navigate to the Page Tab: Click on the 'Page' tab in the top ribbon to access the visual page management tools.
- 3. Insert a New Page: Select the page after which you want to add content, click 'Insert', and choose whether to insert a blank page or append a page from another PDF file.
- 4. Organize Your Document: Drag and drop pages in the thumbnail pane to reorder them exactly as needed without relying on complex JavaScript.

Frequently Asked Questions
Why is my spawned PDF template overwriting the current page?
If you pass this.pageNum as the page index in the spawn() method and do not rename the fields (using the bRename parameter), the spawned page may overlay or conflict with the current page. Passing this.pageNum + 1 safely places it after the current page.
Can I use event.target.page to determine where to spawn a PDF template?
It is generally not recommended. If your PDF has multiple copies of a field with the same name, event.target.page will return an array of page numbers rather than a single integer, causing your spawn script to fail. Use this.pageNum instead.
Do PDF page numbers in JavaScript start at 1?
No, PDF page indexes in JavaScript are zero-based. The first page is index 0. Therefore, this.pageNum returns 0 for the first page, and adding 1 correctly targets the second page position.




