How to Add a PDF Template Page After the Current Page with JavaScript
Question details
The user needs to insert a new PDF page from a hidden template immediately following the currently active page using a JavaScript action.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form where users click an 'Add Page' button to dynamically insert a template page precisely where they are reading, rather than at the end of the file.
- Observed behavior
- By default, calling the JavaScript template spawn method without specific parameters appends the new page to the very end of the document instead of the desired location.
Ensure that your PDF document already contains a hidden template page with the exact name you plan to reference in your JavaScript code.
Use JavaScript to Spawn a Template After the Current Page
Pass the current page number plus one to the spawn method to dictate the exact insertion point.
In Acrobat JavaScript, you can control where a spawned template is placed by providing a page number argument to the spawn() method. Because the page index is zero-based, adding 1 to the current page index calculates the correct position immediately following the active page.
Open your PDF in Form Editing mode, right-click the button you want to trigger the insertion, and select Properties.
Navigate to the Actions tab, select 'Run a JavaScript' from the Select Action dropdown, and click Add.
In the JavaScript Editor window, type the following code: var A = this.getTemplate("Add Page"); A.spawn(this.pageNum + 1);
Click OK to save the script, exit the Form Editing mode, and click your button to verify the template page appears immediately after the active page.

Easily Insert Blank or Template Pages with WPS PDF
If you prefer a simpler approach without writing complex JavaScript, WPS PDF allows you to effortlessly insert blank pages or pages from other files directly into your document using an intuitive interface.
- 1. Open your PDF: Launch WPS Office and open your PDF document.
- 2. Navigate to the Page tab: Click on the 'Page' tab located on the top navigation toolbar.
- 3. Choose the insertion tool: Click the 'Insert' button and select either 'Blank Page' or 'From PDF' depending on your needs.
- 4. Set the location: In the dialog box, choose the exact location to place the new page, such as 'After current page', and click OK.

Frequently Asked Questions
Why does my spawned PDF template always appear at the end of the document?
If you call the spawn() method without passing a page number parameter (e.g., A.spawn()), Acrobat defaults to appending the new page to the end of the PDF. Providing an explicit index overrides this default behavior.
Can I spawn multiple pages from a template at once?
The spawn() method inserts one instance of the template at a time. To insert multiple pages, you must use a loop in your JavaScript code to call the spawn method repeatedly.
Why is my JavaScript button not working in browser PDF viewers?
Many third-party PDF readers and web browsers do not fully support advanced Acrobat JavaScript methods like getTemplate(). For interactive scripts to work reliably, users typically need to open the document in a fully featured PDF editor or viewer.




