How to Insert PDF Templates at a Specific Page Location
Question details
The user needs to know how to insert or spawn PDF template pages at exact locations within a document, particularly when triggered by checkbox selections, to prevent page overlapping.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Automating PDF form creation where specific template pages need to be dynamically added or removed based on user inputs.
- Observed behavior
- Users require a reliable method to identify insertion points via field names or text markers to maintain the correct document flow and page order.
Before inserting templates via scripts, ensure your PDF document is fully structured and that you have identified unique form field names to serve as reliable insertion markers.
Use JavaScript template.spawn() for Dynamic Insertion
The most precise method for automating template insertion based on user actions like checkbox selections.
When dealing with interactive PDF forms, JavaScript allows you to dynamically spawn template pages. To ensure they appear in the correct order, you should identify insertion locations using unique form field names rather than relying solely on page text.
Using the template.spawn() function requires an exact page index. By querying the page location of specific hidden fields, you can dynamically pass the correct page number to the spawn function.
Ensure the page you want to insert is configured as a hidden template within your PDF document's page templates menu.
Navigate to your target form field (e.g., a checkbox), open its properties, and select the Actions tab to add a 'Run a JavaScript' trigger.
Write a script that queries a specific form field name using getField('FieldName').page to determine the exact zero-based index where the new page should be inserted.
Use the script template.spawn({nPage: targetIndex, bRename: true, bOverlay: false}). Setting bOverlay to false ensures the template creates a distinct new page instead of overlapping existing content.

Insert PDF Pages and Templates Easily with WPS Office
While JavaScript automation requires coding knowledge, WPS Office provides a highly intuitive graphical interface for managing, inserting, and reordering PDF template pages without writing a single line of code.
- 1. Open your Document in WPS PDF: Launch WPS Office and open your base PDF document where you want to insert new pages.
- 2. Access Page Management Tools: Navigate to the 'Page' tab on the top ribbon to view all pages in a visual thumbnail layout.
- 3. Insert the Template Page: Click 'Insert Pages' and select 'From PDF' to browse and choose your saved template file.
- 4. Specify the Exact Insertion Location: In the dialog box, specify exactly which page number the template should follow, then click 'OK' to seamlessly insert it without overlapping.

Frequently Asked Questions
Why do my spawned PDF templates overlap instead of creating new pages?
Overlapping usually occurs when the bOverlay parameter in the template.spawn() method is set to true. Change it to false to ensure the template creates a distinct new page at the specified index.
How can I find the correct page index for insertion dynamically?
Instead of hardcoding page numbers, assign a hidden text field at your desired insertion point. Use JavaScript to query that field's page location (using getField), and pass that value to your spawning function.
Can I remove a spawned template page when a user unchecks a box?
Yes. When you spawn a template with bRename: true, the fields on the new page receive a unique prefix. You can use this prefix to identify the dynamically created page and delete it via script when the box is unchecked.
Is it better to use page text or field names to locate insertion points?
Using field names is highly recommended. Page text can be inconsistent and harder to parse via JavaScript, whereas form fields have unique identifiers that can be reliably queried for their exact page number.




