PDF Editing Issues

How to Use JavaScript for Independent Print Date Fields in a PDF Form

Bushra ParveenBushra Parveen Nov 3, 2026 869 views

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.

How to Use JavaScript for Independent Print Date Fields in a PDF Form
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.
Before you start

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.

Solution 1Recommended

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.

1
Open JavaScript Tools

Open your PDF form in your PDF editor. Navigate to the JavaScript tools and select 'Document Actions' or 'Set Document Actions'.

2
Select the Print Event

Choose the 'Document Will Print' event to ensure the script runs exactly when the user initiates a print command.

3
Add the JavaScript Code

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;

4
Adjust Field Names

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.

Add a Print-Event JavaScript with an If Condition
Testing the Script: Print the document to a virtual 'Print to PDF' printer to test the fields without wasting paper. The first print should fill the first field, and the second print should fill the second.
Comprehensive PDF Editor

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. 1. Open the PDF: Launch WPS Office and open your interactive PDF form file.
  2. 2. Access Form Tools: Navigate to the 'Form' tab to interact with, highlight, or fill out the form fields.
  3. 3. Save and Print: Fill in the necessary fields, save your progress, and click 'Print' from the top menu to generate your final document.
Easily fill out interactive PDF forms and manage document text fields.Highly compatible with standard PDF formats and Adobe Acrobat files.Lightweight software with a built-in comprehensive PDF toolkit.Free to download with an intuitive, user-friendly interface.
QA img-9

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.