PDF Editing Issues

How to Dynamically Display the Current PDF Filename in a Form Field

Algirdas JasaitisAlgirdas Jasaitis Oct 28, 2026 869 views

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.

How to Display the Current PDF Filename in a PDF Form Field
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.
Before you start

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.

Solution 1Recommended

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.

1
Identify the target form field

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

2
Open Document JavaScript settings

Navigate to your PDF editor's interactive form or JavaScript tools menu and select 'Document JavaScripts' or 'Document-Level Scripts'.

3
Add the script

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

4
Save and test

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.

Use a Document-Level Script to Update the Form Field
Script Execution: The field will now automatically fetch and display the current filename every time the document is opened.

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. 1. Open your PDF in WPS Office: Launch WPS Office and open your PDF document. Switch to the 'PDF' tab at the top.
  2. 2. Access form tools: Navigate to the 'Form' tab on the ribbon to reveal all interactive form controls.
  3. 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.
Easily insert and customize interactive PDF form fieldsFully compatible with standard PDF formats and Adobe Acrobat formsLightweight, fast, and user-friendly interface for seamless editing
microsoft office alternative - wps office

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.