How to Add Dynamic Page Numbers to Spawned PDF Pages
Question details
The user needs to update the 'Page X of Y' numbering automatically when new pages are generated from a hidden template in a PDF form.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating dynamic PDF forms where users can spawn additional pages from a template without breaking the page numbering sequence.
- Observed behavior
- Static page numbers or default fields do not update automatically when new pages are spawned, requiring a script to recalculate the total page count and current page index.
Ensure you have access to the JavaScript console or document-level scripting tools in your PDF editor. Familiarity with basic PDF form field properties and JavaScript is recommended.
Implement a Document-Level JavaScript for Page Numbering
Use a custom JavaScript function to loop through all pages and update the text fields immediately after a new page is spawned.
To ensure page numbers update correctly on dynamically spawned pages, you need a script that recounts the total number of pages and assigns the correct index to each page. This script must be executed every time the spawn action occurs.
Open your PDF form in editing mode and add a text field named 'PageNumber' to your hidden template page. Make sure this field is set to be read-only.
Open the JavaScript tools in your PDF editor and create a new Document-Level script.
Write a JavaScript function that iterates from 0 to 'this.numPages - 1'. Inside the loop, target the 'PageNumber' field on each page and set its value to '(i + 1) + " of " + this.numPages'.
Locate the button or script that triggers your page spawn action. Add a call to your newly created page numbering function at the very end of that spawn script so it recalculates immediately.

Easily Add and Manage Page Numbers in WPS PDF
While advanced JavaScript is required for dynamically spawned pages in Acrobat, WPS PDF offers an intuitive interface to easily insert, update, and format page numbers across your entire document without writing any code.
- 1. Open PDF in WPS: Launch WPS Office and open your PDF document using the WPS PDF editor.
- 2. Access the Insert Tab: Navigate to the 'Insert' tab on the top ribbon and select the 'Page Number' tool.
- 3. Configure Page Numbers: Choose your preferred page number format, select the position on the page, and click apply to update the entire document instantly.

Frequently Asked Questions
Why aren't my PDF page numbers updating automatically when I add a new page?
Standard static text used for page numbers doesn't recalculate when a document's structure changes. You must use form fields combined with a JavaScript calculation (like this.numPages) to force the document to update the count dynamically.
What is a hidden template in a PDF form?
A hidden template is a pre-designed page within a PDF that is not visible by default. Using JavaScript, users can 'spawn' or duplicate this hidden page to add more sections to the form dynamically as needed.
How do I trigger the page numbering script after spawning a page?
You should append your custom page-numbering function call directly to the end of the button action or script that triggers the spawn() method. This ensures the numbers recalculate immediately after the new page is created.




