PDF Editing Issues

How to Spawn a PDF Template at the End of a Document

John WilsonJohn Wilson Oct 28, 2026 868 views

Question details

The user wants to spawn a new page from a PDF template and append it to the end of the document rather than inserting it after the first page.

How to Spawn a PDF Template at the End of a Document
Product
PDF Editor
Device & OS
not provided
Scenario
Using a JavaScript button to dynamically generate new pages from a hidden or visible template within a PDF form.
Observed behavior
The script successfully spawns the template, but the new page appears immediately after page 1 instead of at the document's end.
Before you start

Ensure your PDF editor supports JavaScript for document actions and that the template page you are trying to spawn is properly named and configured within the document.

Solution 1Recommended

Use JavaScript to Spawn Template at the End Using numPages

Modify your JavaScript code to target the numPages property, which dynamically points to the end of the document.

When using the spawn() method in PDF JavaScript, the nPage parameter determines where the new page is inserted. If set incorrectly, it defaults to placing it after the current or first page. By setting nPage to this.numPages, you instruct the script to append the newly spawned page precisely after the last page of the document.

1
Access the button properties

Open your PDF form in editing mode, right-click the button that triggers the action, and select Properties.

2
Edit the JavaScript action

Navigate to the Actions tab, select 'Run a JavaScript', and click 'Edit' to modify your existing script.

3
Update the spawn code

Replace your spawn command with the following snippet: var template = this.getTemplate("YourTemplateName"); template.spawn({nPage: this.numPages, bRename: true, bOverlay: false});

4
Adjust field resetting

If you are using resetFieldsOnPage(), ensure it points to the new page index so fields are cleared accurately based on the newly spawned page.

Use JavaScript to Spawn Template at the End Using numPages
Index Numbering: Remember that PDF page indices usually start at 0, while this.numPages returns the total count. Using nPage: this.numPages effectively places the new page after the last existing index.
Advanced PDF Editing with WPS Office

Easily Manage and Insert PDF Pages with WPS Office

While JavaScript provides dynamic page spawning, WPS PDF offers an intuitive, code-free interface to insert, extract, and manage PDF pages and templates effortlessly.

  1. 1. Open your PDF in WPS Office: Launch WPS Office and open your PDF document using the WPS PDF editor.
  2. 2. Navigate to the Page tab: Click on the 'Page' tab in the top ribbon to access comprehensive page management tools.
  3. 3. Insert a new page: Click 'Insert Pages' and choose whether to insert a blank page or a page from another file, then simply select 'At the end of the document' as the insertion point.
Insert, extract, and manage PDF pages visually without writing JavaScript.Fully compatible with Microsoft Office formats (Word, Excel, PowerPoint) and standard PDFs.Lightweight, fast, and user-friendly interface for seamless document editing.
QA img-9

Frequently Asked Questions

Why does my spawned PDF template overwrite existing pages?

This usually happens if the bOverlay parameter in the spawn() method is set to true. Change it to false to ensure the template is inserted as a new, standalone page rather than overlaying the current one.

How do I rename fields on the newly spawned PDF page?

Set the bRename parameter to true in your spawn() function. This automatically prefixes the field names on the new page, preventing them from mirroring the data of the original template fields.

Can I hide the PDF template so it doesn't show in the main document?

Yes, you can set the template page to be hidden in your PDF editor's page properties. The getTemplate() and spawn() JavaScript methods can still access and generate new pages from hidden templates.

What is the difference between nPage and this.numPages?

nPage is the parameter used to specify the target page index for insertion. this.numPages is a document property that returns the total number of pages. Passing this.numPages to nPage ensures the new page is added at the very end of the file.