How to Auto-Populate and Lock PDF Date Fields via JavaScript
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.

- 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.
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.
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.
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.
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.
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.
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.

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. Open WPS PDF: Launch WPS Office and open your PDF document using the WPS PDF tool.
- 2. Access Form Features: Navigate to the 'Form' tab on the top ribbon to view all interactive form fields within your document.
- 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. Save the Document: Click 'Menu' and select 'Save As' to export the completed and locked PDF form.

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.




