How to Automatically Increment Dates in Spawned Acrobat PDF Pages
Question details
The user wants to create a PDF staff timesheet that spawns a new template page and automatically calculates the new week and daily dates based on the previous page's input.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Automating a staff timesheet PDF so that adding a new week correctly increments the date fields and places the new page in the desired order.
- Observed behavior
- The current script spawns new pages at the end of the document instead of the beginning and does not automatically calculate the progressive dates for the new week.
Ensure you have access to Adobe Acrobat Pro, as Acrobat Reader does not support creating, editing, or testing advanced document-level JavaScript and page templates.
Use Custom JavaScript for Page Spawning and Date Calculation
Applying a custom Acrobat JavaScript allows you to insert the spawned page at a specific index and mathematically increment the date fields.
To achieve automatic date calculation and correct page placement, you must bypass the default template spawning behavior and write a custom script. This involves using the getTemplate() method, specifying the nPage parameter, and using JavaScript Date objects to add 7 days to the previous week's date.
Open your PDF timesheet in Adobe Acrobat Pro, navigate to the Tools menu, and select 'Prepare Form'.
Add a new Button field to your document. Right-click the button, select 'Properties', go to the 'Actions' tab, choose 'Run a JavaScript' from the Select Action dropdown, and click 'Add'.
In the JavaScript editor, define your template object. Use the code 'var t = this.getTemplate("YourTemplateName"); t.spawn({nPage: 0, bRename: true, bOverlay: false});' to spawn the page at the very beginning of the document.
Below the spawn command, write variables to retrieve the 'WeekBeginning' field value from the previous page. Convert it to a JavaScript Date object, add 7 days (7 * 24 * 60 * 60 * 1000 milliseconds), format the new date, and assign it to the newly spawned page's date field.

Easily Manage and Edit PDF Timesheets with WPS Office
If writing complex JavaScript for page spawning is too tedious, you can easily manage, duplicate, and edit PDF timesheets manually using WPS Office's intuitive PDF tools.
- 1. Open your timesheet: Launch WPS Office and open your existing PDF timesheet document.
- 2. Duplicate the template page: Navigate to the 'Page' tab on the top ribbon, select your timesheet page, and use the 'Extract' or 'Insert' tools to easily duplicate the blank template into the desired position.
- 3. Fill in the new dates: Switch to the 'Form' or 'Edit' tab to click into the date fields and quickly type in the dates for the new week.
- 4. Save the updated file: Click 'Save' or 'Save As' to store your updated weekly timesheet to your local drive or WPS Cloud.

Frequently Asked Questions
Why are my spawned pages appearing at the end of the PDF?
By default, Acrobat's spawn method appends pages to the end of the document. You must specify the 'nPage' parameter in your JavaScript (e.g., nPage: 0) to insert it at a specific position, such as the beginning.
Can I auto-increment dates without JavaScript in Acrobat?
No, automatically calculating and populating future dates based on a previous page's input strictly requires custom JavaScript to perform the necessary date math.
How do I handle overlapping field names when spawning pages?
Ensure that the 'bRename' parameter in your spawn function is set to true. This automatically prefixes the field names with the page number (e.g., P1.Template.FieldName), preventing the new inputs from overriding the old ones.




