PDF Editing Issues

How to Automatically Update a PDF Form Field with the File Name

Nimra MalikNimra Malik Nov 3, 2026 870 views

Question details

The user needs a method to automatically populate a specific PDF form field with the document's file name and wants to know how to keep it updated if the file is renamed.

How to Automatically Update a PDF Form Field with the File Name
Product
PDF Editor
Device & OS
not provided
Scenario
Creating dynamic PDF forms where the current file name needs to be displayed as text directly inside a designated form field.
Observed behavior
The user wants the file name to automatically insert into the field, but notices that if the document is closed and renamed in the operating system, the form field retains the old name upon reopening.
Before you start

Identify the exact field name in your PDF where you want the file name to appear, and ensure your PDF editor supports executing document-level JavaScript.

Solution 1Recommended

Use a Document-Level JavaScript to Populate the Field

Add custom JavaScript to the PDF document to automatically retrieve the file name and apply it to the designated text field.

PDF applications do not automatically rewrite saved field values when a file is renamed outside the application (e.g., via Windows File Explorer or Mac Finder). By setting up a document-level script, you instruct the PDF to fetch the new file name when the file is opened.

1
Access the JavaScript Editor

Open your PDF in an advanced editor. Navigate to the JavaScript tools, often found under Tools > Form or Tools > JavaScript, and select 'Document JavaScripts'.

2
Add the Script

Create a new script and enter the following code: this.getField("YourFieldName").value = this.documentFileName; (Replace 'YourFieldName' with the exact name of your target text field).

3
Set a Trigger to Refresh

To ensure the field updates when the file is renamed, add a 'Page Open' action to the first page of your PDF. Go to Page Properties > Actions, select 'Run a JavaScript', and paste the same script so it executes every time the document is opened.

Use a Document-Level JavaScript to Populate the Field
Updating Renamed Files: Because the file system rename occurs outside the PDF environment, the internal field data won't reflect the change until the script is triggered again upon the next opening.
Efficient PDF Form Editing

Easily Manage and Edit PDF Forms with WPS PDF

WPS Office offers a comprehensive PDF editor that empowers you to easily create, edit, and manage interactive PDF forms. With support for robust form fields, you can seamlessly streamline your document workflows and data collection.

  1. 1. Open WPS Office: Launch WPS Office and open your existing PDF document using the built-in WPS PDF tool.
  2. 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to access the various form creation and editing tools.
  3. 3. Edit Form Fields: Click 'Text Field' to insert a new field, or double-click an existing field to modify its properties and accurately set its designated name.
  4. 4. Save the PDF: Once your interactive form is configured and your fields are named properly, click 'Save' to retain all form properties.
Easily add, edit, or delete text fields, checkboxes, and drop-down menus in your PDF forms.Highly compatible with Adobe Acrobat PDF forms and standard PDF formats.Lightweight and fully integrated into the WPS Office suite for centralized document management.Support for interactive form elements to enhance document usability.
microsoft office alternative - wps office

Frequently Asked Questions

Why doesn't the PDF form field update immediately when I rename the file?

PDF software saves form field values internally as static text. If you rename the file via your operating system while the document is closed, the internal field data isn't modified. A script must be triggered upon opening the file to detect and rewrite the new name.

How do I find the correct name of my PDF form field?

Open your PDF in form editing mode. Right-click the target text field and select 'Properties'. The 'Name' field found in the General tab is the exact name you need to reference inside your JavaScript code.

Can I use this JavaScript on any PDF reader?

No, not all standard PDF readers support JavaScript execution. You will need an advanced PDF viewer or editor that allows running document-level or form-level scripts to see the automatic file name update.

How do I ensure the script runs automatically every time?

You can tie the script to a 'Page Open' action. By accessing the Page Properties of the first page of your PDF, you can set the action to 'Run a JavaScript' and input your code there, ensuring it runs immediately when a user views the file.