How to Remove the PDF Extension from a File Name in Acrobat Forms
Question details
The user needs to display the current PDF document's name inside a form field but wants to automatically exclude the .pdf extension from the visible text.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Automatically populating a text field with the document's file name using a custom JavaScript calculation.
- Observed behavior
- By default, the documentFileName property returns the full file name including the .pdf extension, which requires an additional script to strip away.
Ensure you are using a PDF editor that supports form editing and JavaScript execution. You may also need to enable JavaScript in your software preferences to properly test the script.
Use JavaScript with a Case-Insensitive Regular Expression
Apply a JavaScript regex replacement to securely and cleanly strip the extension regardless of its capitalization.
When working with Acrobat forms, the documentFileName property fetches the complete name of the file. By utilizing the replace() method combined with a regular expression, you can target the .pdf portion at the very end of the string and replace it with an empty string.
Using a case-insensitive regular expression ensures that the script works perfectly even if the file is saved as .PDF or .Pdf.
In Prepare Form mode, right-click the target text field (for example, 'Text1') where you want the file name to appear, and select 'Properties'.
Click on the 'Calculate' tab located in the Properties window and select the 'Custom calculation script' option.
Click the 'Edit' button and input the following script: this.getField("Text1").value = this.documentFileName.replace(/\.pdf$/i, "");
Click 'OK', close the properties dialog box, and save the document. The text field should now populate with the file name minus the .pdf extension.
Easily Manage and Edit PDF Forms with WPS Office
If you need a reliable and intuitive solution for handling PDF forms, WPS Office provides a comprehensive PDF editor. You can view, fill out, and manage interactive PDF documents effortlessly without worrying about complex scripting.
- 1. Open your PDF in WPS Office: Launch WPS Office and open the PDF form you wish to edit or fill out.
- 2. Access the Form Tools: Navigate to the 'Form' tab on the top ribbon to view available interactive field options.
- 3. Modify Form Properties: Double-click any form field to open its properties menu, where you can adjust text formatting, defaults, and appearance.
- 4. Save and Export: Click 'Save' to apply your modifications, ensuring full compatibility and easy sharing across different PDF viewers.

Frequently Asked Questions
Why is my JavaScript calculation not updating the field automatically?
Calculations typically trigger when a form value changes. If the script relies purely on the document opening, you may need to apply the script as a 'Page Open' action rather than a custom calculation script to ensure it triggers immediately.
Will this script remove the extension if it is saved in uppercase (.PDF)?
Yes. The 'i' flag at the end of the regular expression ( /\.pdf$/i ) ensures that the search is case-insensitive, meaning it will successfully remove .pdf, .PDF, or .Pdf.
Can I extract just the file name without the full computer file path?
Yes. The documentFileName property natively returns only the base file name (e.g., 'invoice.pdf'), not the full directory path. Stripping the extension is all that is required to isolate the name itself.




