PDF Editing Issues

How to Copy a PDF Page and Rename Form Fields Using JavaScript

John WilsonJohn Wilson Nov 5, 2026 871 views

Question details

The user needs to duplicate a PDF form page using JavaScript templates, place it after a specific page, and successfully calculate totals across both the original and newly spawned form fields.

How to Copy a PDF Page and Rename Form Fields Using JavaScript
Product
PDF Editor
Device & OS
not provided
Scenario
Creating dynamic PDF forms that require duplicating template pages and summing up data from automatically renamed form fields.
Observed behavior
The user needs the correct JavaScript syntax to spawn a template page at a specific index and a method to logically loop through and calculate values from predictably renamed fields on the spawned pages.
Before you start

Ensure that your PDF editor supports advanced JavaScript execution and document-level scripting. Before running scripts that alter the document structure, save a backup copy of your PDF.

Solution 1Recommended

Using JavaScript Templates to Spawn Pages and Calculate Totals

Utilize the getTemplate and spawn JavaScript methods to duplicate your target page, then use document-level scripting to aggregate field values.

In advanced PDF editors, you can automate page duplication using JavaScript. By calling a pre-defined template, you can insert new pages dynamically. It is crucial to set the rename parameter to true so that new fields are generated with unique, predictable names, allowing calculation scripts to identify them correctly.

1
Identify the target page template

Determine the zero-based index of the page you want to copy. For example, if you want to place the copied page immediately after page 16, its zero-based index for the script will be 15.

2
Write the spawn script

Execute the spawn command targeting your specific template. Use the code: var t1 = getTemplate("Anlage"); t1.spawn({nPage: 15, bRename: true, bOverlay: false});. Setting bRename to true ensures the form fields on the new page receive unique names.

3
Identify the newly renamed fields

Note the predictable naming convention of the newly spawned fields (e.g., "Amount 1", "Amount 2"). This consistency allows you to target them easily with string manipulation in your subsequent scripts.

4
Create the calculation script

Write a document-level calculation script that loops through both the original and the spawned field names. Use the Number() function to convert their text values into numeric formats, and sum them together into your designated total field.

Using JavaScript Templates to Spawn Pages and Calculate Totals
Zero-Based Page Indexing: Remember that PDF JavaScript page indexes start at 0. You must always subtract 1 from your physical page number when referencing pages in your scripts.
Manage PDFs Effortlessly

Create and Edit PDF Forms Easily with WPS Office

WPS Office provides a comprehensive PDF toolkit that allows you to easily edit PDF documents, insert pages, and manage interactive forms without needing to write complex JavaScript code.

  1. 1. Open your PDF: Launch WPS Office and open your PDF document using the built-in PDF editor.
  2. 2. Access Page Management: Navigate to the 'Page' tab on the top ribbon to visually duplicate, insert, or extract pages without writing any code.
  3. 3. Edit Form Fields: Go to the 'Form' tab to manually add, rename, or configure text fields and setup calculation properties seamlessly.
Visually duplicate PDF pages and edit form fields without complex scriptsFully compatible with standard PDFs and Microsoft Office formats (Word, Excel, PowerPoint)Lightweight suite with intuitive tools for merging, splitting, and organizing pages
microsoft office alternative - wps office

Frequently Asked Questions

Why do my spawned PDF form fields have the exact same values as the original?

If you duplicate a page without renaming the fields, the new fields will share the exact same names and thus share the same data. Ensure you set the parameter 'bRename: true' in your spawn script to make them independent.

How do I reference a specific page number in PDF JavaScript?

PDF JavaScript uses zero-based indexing for document pages. To interact with the 1st page, you use index 0. To place a spawned template after page 16, you would use 'nPage: 15'.

What happens if I set bOverlay to true when spawning a template?

If 'bOverlay' is set to true, the template's contents will be drawn directly on top of the existing page specified by 'nPage', merging the visual elements rather than creating a brand new independent page.