PDF Editing Issues

How to Auto-Populate and Lock PDF Date Fields via JavaScript

John WilsonJohn Wilson Oct 28, 2026 869 views

Question details

The user needs to know how to use JavaScript to automatically fill PDF form fields with the current date, make them read-only when other fields are populated, and properly restore the current date if the form is reset.

How to Auto-Populate and Lock PDF Date Fields via JavaScript
Product
PDF Editor
Device & OS
not provided
Scenario
Creating dynamic and secure PDF forms that require automatic date stamping and locking mechanisms to prevent post-completion modifications.
Observed behavior
The goal is to automatically display today's date in specific form fields, lock these fields once the user completes the rest of the form, and reset the date correctly when the form is cleared.
Before you start

Ensure you are using a PDF editor that supports document-level JavaScript execution and form field property editing. Familiarity with basic JavaScript syntax is recommended.

Solution 1Recommended

Configure PDF Form Using Acrobat JavaScript

Use JavaScript attached to document actions and form fields to control the date population, set default values, and trigger locking behaviors.

By setting the defaultValue property of a date field upon opening the document, you ensure that the standard form Reset button will revert the field to today's date rather than clearing it completely.

1
Set the Default Date

Open your PDF editor's JavaScript panel and add a document-level script that runs when the PDF opens. Use this script to define the current date and assign it to the 'defaultValue' property of your date field.

2
Add Field Validation Logic

Right-click the other form fields (such as a signature or final input field) and open their Properties. Navigate to the Calculate or Validate tab to insert a custom JavaScript that checks if the field is empty.

3
Lock the Date Field

Within your validation script, add a condition that targets your date field. Set its 'readonly' property to true (e.g., this.getField("Date").readonly = true;) when the required form fields contain values.

4
Configure the Reset Button

Create a Reset Form button. Since you already assigned today's date to the 'defaultValue' upon opening, clicking this button will clear user inputs but correctly restore the current date while unlocking the field.

Configure PDF Form Using Acrobat JavaScript
JavaScript Syntax Tip: To set the default date, use syntax similar to: this.getField("DateFieldName").defaultValue = util.printd("mm/dd/yyyy", new Date());

Easily Manage PDF Forms and Fields with WPS Office

WPS Office features a robust PDF editor that allows you to easily fill, manage, and secure PDF forms. While complex JavaScript is one way to lock fields, WPS PDF offers intuitive built-in tools to secure your documents without coding.

  1. 1. Open WPS PDF: Launch WPS Office and open your PDF document using the WPS PDF tool.
  2. 2. Access Form Features: Navigate to the 'Form' tab on the top ribbon to view all interactive form fields within your document.
  3. 3. Fill and Secure: Fill in the required information. To prevent further editing, go to the 'Protect' tab and apply editing restrictions or a permissions password.
  4. 4. Save the Document: Click 'Menu' and select 'Save As' to export the completed and locked PDF form.
Easily create, edit, fill, and sign interactive PDF forms.Set document permissions and passwords to quickly lock completed forms.Seamlessly compatible with Microsoft Office formats (Word, Excel) and Adobe PDF.
QA img-9

Frequently Asked Questions

Why isn't the date field updating to today's date when I open the PDF?

This usually happens if the JavaScript is set as a calculation script rather than a document-level script. Ensure your script is added under 'Document Actions' or 'Document JavaScripts' so it executes the moment the file initializes.

Can I format the date differently using JavaScript?

Yes, you can change the date format by adjusting the util.printd function parameters. For example, using "dd-mmm-yyyy" will display the date as 01-Jan-2023 instead of the standard numerical format.

How do I unlock the field again if I make a mistake?

If your script is tied to a specific field's validation, clearing that field should trigger the 'else' part of your JavaScript to set the readonly property back to false. Alternatively, clicking the Reset Form button will clear the triggers and unlock the fields automatically.