How to Use JavaScript for Independent Print Date Fields in a PDF Form
Question details
The user wants to use JavaScript to automatically populate different date fields based on whether the document is being printed for the first time or subsequently.

- Product
- Adobe Acrobat / PDF Form
- Device & OS
- not provided
- Scenario
- Configuring a PDF form with print-event JavaScript so that the first print logs a date in one specific field, and any later prints log the date in a second distinct field.
- Observed behavior
- Needs a script with a conditional statement to check if the first date field is empty before populating either the first or second date field upon printing.
Ensure your PDF form already has the required text fields created and appropriately named (e.g., 'Text6' and 'Text8'). You will need access to the document-level or print-event JavaScript settings in your advanced PDF editor.
Add a Print-Event JavaScript with an If Condition
Use this script to check if the first date field is empty. If it is, populate it with the current date; if not, populate the second date field.
By utilizing the 'Document Will Print' action in your PDF editor, you can trigger a JavaScript check right before the print job is sent. The script determines if the primary date field already contains a value to decide where to insert the current timestamp.
Open your PDF form in your PDF editor. Navigate to the JavaScript tools and select 'Document Actions' or 'Set Document Actions'.
Choose the 'Document Will Print' event to ensure the script runs exactly when the user initiates a print command.
Click 'Edit' and paste the following code: var f1 = this.getField('Text6'); var f2 = this.getField('Text8'); var s = util.printd('m/d/yyyy h:MM tt', new Date()); if (f1.valueAsString == '') f1.value = s; else f2.value = s;
Change 'Text6' and 'Text8' in the code to match the actual names of the text fields you created in your PDF form. Click 'OK' to save your changes.

Edit and Manage PDF Forms Easily with WPS Office
WPS Office provides a robust PDF editor that allows you to read, annotate, edit, and fill PDF forms effortlessly. Experience seamless document management and extensive format support across all your devices.
- 1. Open the PDF: Launch WPS Office and open your interactive PDF form file.
- 2. Access Form Tools: Navigate to the 'Form' tab to interact with, highlight, or fill out the form fields.
- 3. Save and Print: Fill in the necessary fields, save your progress, and click 'Print' from the top menu to generate your final document.

Frequently Asked Questions
Why isn't my JavaScript updating the date field when printing?
Ensure that the script is tied specifically to the 'Document Will Print' action rather than a button click or document open event. Also, verify that the field names in the script perfectly match the names of the text fields in your form.
Can I use a different date format in my PDF script?
Yes. In the util.printd function, you can change the format string. For example, changing 'm/d/yyyy h:MM tt' to 'dd-mmm-yyyy' will display the date as 15-Aug-2023 instead.
How do I clear the date fields if I need to reset the form?
You can create a 'Reset Form' button using the PDF form tools and set its action to reset specific text fields (e.g., Text6 and Text8). This will clear the values, allowing the print script to start over at the first field.
Will this JavaScript work on mobile PDF readers?
Most mobile PDF readers have limited or no support for the Adobe JavaScript API and print events. This script is best utilized on desktop PDF software that fully supports Acrobat JavaScript.




