How to Dynamically Display the Current PDF Filename in a Form Field
Question details
The user wants a PDF form field to automatically update and display the current document filename, even after the file has been renamed.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating or editing an interactive PDF form that needs to reference its own filename dynamically.
- Observed behavior
- When code is placed in a field calculation event, it may not trigger when the document opens, causing the form field to display an old filename after the document is renamed.
Ensure you are using a professional PDF editor that supports form creation and JavaScript execution, as basic PDF readers may block scripts for security reasons.
Use a Document-Level Script to Update the Form Field
Moving the filename extraction code to a document-level script ensures it runs immediately when the PDF is opened, accurately reflecting any recent file name changes.
Placing JavaScript in a standard field calculation event often fails because those events wait for user interaction or specific form data changes to trigger. A document-level script executes as soon as the PDF document is initialized.
Open your PDF form in edit mode and note the exact name of the text field where you want the filename to appear (e.g., 'FilenameField').
Navigate to your PDF editor's interactive form or JavaScript tools menu and select 'Document JavaScripts' or 'Document-Level Scripts'.
Create a new script, name it (e.g., 'UpdateFilename'), and enter the following code: this.getField("YourFieldName").value = this.documentFileName; (replace 'YourFieldName' with your actual field name).
Click 'OK' to save the script, then save and close the PDF. Rename the file in your file explorer and open it again to verify the field updates successfully.

Create and Manage PDF Forms Easily with WPS Office
WPS PDF provides comprehensive tools to create, edit, and manage interactive PDF forms. You can easily insert text fields, checkboxes, and configure advanced form properties to automate your document workflows.
- 1. Open your PDF in WPS Office: Launch WPS Office and open your PDF document. Switch to the 'PDF' tab at the top.
- 2. Access form tools: Navigate to the 'Form' tab on the ribbon to reveal all interactive form controls.
- 3. Add and configure fields: Click 'Text Field' to draw a new field on your document, then double-click it to adjust properties, appearance, and actions.

Frequently Asked Questions
Why isn't my PDF form field updating after I rename the file?
Calculation events might not trigger automatically when a document is opened. A document-level script is required to force the update as soon as the file loads into the PDF viewer.
Can I remove the file extension (.pdf) from the displayed filename?
Yes, you can modify the JavaScript string before assigning it to the field value. For example, use this.documentFileName.replace(/\.pdf$/i, "") to strip the extension.
Do all PDF viewers support JavaScript in forms?
No, some basic web browsers and mobile PDF viewers disable JavaScript for security or performance reasons. Users will need a dedicated PDF editor like WPS Office or Adobe Acrobat to run document-level scripts properly.




