How to Set a PDF Expiry Date from the First Open Date
Question details
The user needs a PDF form script that calculates an expiration date the first time a document is opened, writes it to form fields, and preserves those initial dates for all future openings.

- Product
- Device & OS
- not provided
- Scenario
- Adding a document-level script to a PDF form to track when the file was first opened and establish a permanent expiration date.
- Observed behavior
- Without specific conditions, the script recalculates the current and expiration dates every time the PDF is opened, rather than locking in the date from the initial opening.
Ensure you have access to a PDF editing tool that supports creating form fields and executing document-level JavaScript code.
Use Document-Level JavaScript with Empty-Field Conditions
Implement a script that only calculates the date when the target form fields are empty, sets the fields to read-only, and requires the user to save the document.
To prevent dates from recalculating upon every opening, the script must verify if a date already exists. Once the initial dates are generated, locking the specific field objects ensures they cannot be manually changed. However, remember that client-side PDF dates are not a secure digital rights management (DRM) mechanism. Users can easily bypass this by saving a copy of the original, unopened blank file and starting over.
Open your PDF form editor and insert text fields designated for the current date and the expiration date. Note the exact field names used.
In your document-level JavaScript, wrap your date calculation in an 'if' statement that checks whether the date fields are currently empty. If they contain data, the script should bypass the calculation.
When formatting your output dates within the script, use a four-digit year format (e.g., "yyyy") instead of "yy" to prevent ambiguous date bugs and formatting errors.
Instead of using 'event.target.readonly', explicitly target the field variables (for example, 'f.readonly = true' and 'dateField.readonly = true') to lock the fields once the dates are written.
The mechanism relies on the file being saved. Instruct users to save the document immediately after opening it for the first time so the locked dates become a permanent part of the file.

Manage PDF Forms and Security with WPS Office
WPS Office offers a comprehensive set of PDF tools, allowing you to easily create interactive forms, manage text fields, and configure document encryption settings to enhance your document's security.
- 1. Open WPS Office PDF: Launch WPS Office and open your existing PDF document or create a new one.
- 2. Navigate to the Form tab: Select the 'Form' tab from the top ribbon to access the interactive form creation tools.
- 3. Insert Form Fields: Click 'Text Field' to insert areas for your dates, then double-click the fields to adjust their properties and names.
- 4. Secure the PDF: Go to the 'Protect' tab to encrypt the document and restrict editing permissions, ensuring users cannot tamper with the fields manually.

Frequently Asked Questions
Why does the date recalculate every time I open the PDF?
If the document-level script lacks a condition to check whether the field already contains a date, it will execute every time the document loads. You must add an 'if empty' check before calculating and writing the date.
Why doesn't event.target.readonly work in my document script?
The 'event.target' property refers to the specific element triggering an event. For scripts running at the document level when the file opens, you must reference the specific field object directly, such as 'getField("DateFieldName").readonly = true'.
Is setting an expiry date with JavaScript completely secure?
No. Client-side expiration relies entirely on the user saving the file after the first open. A user can easily bypass this mechanism by keeping a backup of the original, unopened document or choosing not to save changes.




