How to Prevent a PDF Timestamp from Changing When Reopened
Question details
The user needs to generate a unique date/time stamp in a PDF field via JavaScript, but wants to prevent the timestamp from updating automatically every time the file is opened.

- Product
- Device & OS
- not provided
- Scenario
- Generating a unique project-number or timestamp in a PDF form upon document creation or initial save.
- Observed behavior
- The timestamp script re-executes every time the PDF is opened, overriding the previously saved date and time value.
Ensure you have access to a PDF editor that supports JavaScript form formatting, and locate the exact name of the text field you are using to store the timestamp.
Add a Conditional Check to the JavaScript Code
Wrap your timestamp generation script in an 'if' statement to ensure it only runs when the field is currently empty.
When a PDF executes a JavaScript code on open or print, it runs blindly unless instructed otherwise. By checking if the field is empty first, the script will only insert the current date and time the very first time it is triggered. Afterwards, since the field contains a value, the script skips the generation, locking the timestamp.
Enter the form editing mode in your PDF editor, right-click the text field you want to use for the timestamp (e.g., 'UniqueNumber'), and select 'Properties'.
Navigate to the 'Calculate' or 'Action' tab depending on your trigger method, select 'Custom calculation script', and click 'Edit'.
Enter the following script: if (this.getField('UniqueNumber').valueAsString == '') { this.getField('UniqueNumber').value = util.printd('yymmddHHMM', new Date()); }
Click 'OK' to save the script. Save the PDF document, close it, and reopen it to verify that the timestamp remains unchanged.

Edit and Fill PDF Forms with WPS PDF
WPS Office provides a comprehensive PDF toolset, allowing you to easily read, fill, edit, and sign PDF forms. If you're dealing with complex form fields and need a lightweight, fast, and highly compatible alternative, try WPS PDF.
- 1. Download and Install: Download WPS Office for free from the official website and install it on your device.
- 2. Open Your PDF: Launch WPS Office and open your PDF document with the built-in PDF reader.
- 3. Fill and Save: Easily interact with form fields, fill in required data, and save your document securely without unwanted alterations.

Frequently Asked Questions
Why does my PDF timestamp update every time I print it?
If your JavaScript is set to trigger on a 'Will Print' action without a conditional check, the script will fetch the current date and time during every print job. Applying the empty-field condition prevents this recalculation.
Can I use this script for other date formats?
Yes, the util.printd function allows various date formats. For example, changing 'yymmddHHMM' to 'mm/dd/yyyy' will output the date in a standard US date format.
Will this JavaScript work on all PDF readers?
Most desktop PDF editors support basic Acrobat JavaScript. However, some simplified mobile PDF viewers or web browser extensions may not execute JavaScript at all, which means the field might remain blank in those specific environments.




