PDF Editing Issues

How to Add a PDF Template Page After the Current Page with JavaScript

Olivia MillerOlivia Miller Nov 5, 2026 869 views

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.

How to Add a PDF Template Page After the Current Page with JavaScript
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.
Before you start

Ensure that your PDF document already contains a hidden template page with the exact name you plan to reference in your JavaScript code.

Solution 1Recommended

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.

1
Prepare the button action

Open your PDF in Form Editing mode, right-click the button you want to trigger the insertion, and select Properties.

2
Add a JavaScript action

Navigate to the Actions tab, select 'Run a JavaScript' from the Select Action dropdown, and click Add.

3
Enter the script

In the JavaScript Editor window, type the following code: var A = this.getTemplate("Add Page"); A.spawn(this.pageNum + 1);

4
Save and test

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.

Use JavaScript to Spawn a Template After the Current Page
Zero-Based Index System: The this.pageNum property counts the first page as 0. By using this.pageNum + 1, you correctly target the position right after the currently viewed page.
Easy PDF Editing

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. 1. Open your PDF: Launch WPS Office and open your PDF document.
  2. 2. Navigate to the Page tab: Click on the 'Page' tab located on the top navigation toolbar.
  3. 3. Choose the insertion tool: Click the 'Insert' button and select either 'Blank Page' or 'From PDF' depending on your needs.
  4. 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.
No JavaScript or coding required for basic page insertionFully compatible with standard Adobe PDF formatsIntuitive drag-and-drop page management interfaceLightweight and runs smoothly on all major operating systems
microsoft office alternative - wps office

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.