PDF Editing Issues

How to Automatically Display PDF File Name on All Pages

Elise WilliamsElise Williams Nov 24, 2026 868 views

Question details

The user wants to automatically insert the PDF's file name (excluding the .pdf extension) into the header or footer of every page in the document.

How to Automatically Display the PDF File Name on All Pages
Product
PDF
Device & OS
not provided
Scenario
Adding dynamic document information to headers or footers during processing.
Observed behavior
The user needs a method or script to extract the file name and display it consistently across all pages of the PDF.
Before you start

Ensure you have a PDF editor installed that supports form fields and custom JavaScript execution, or advanced header and footer customization.

Solution 1Recommended

Use a Custom JavaScript Calculation in a Text Field

Create a read-only text field and apply a custom script to automatically extract and display the file name.

This method uses a dynamic form field that reads the document's properties. By splitting the file name string, you can isolate the name from its extension and display it cleanly.

1
Add a Text Field

Open your PDF editor's form tools and insert a text field in the header or footer area where you want the file name to appear.

2
Set to Read-Only

Open the text field properties and check the 'Read-Only' option so the text cannot be manually altered by viewers.

3
Add Custom Calculation Script

Go to the Calculate tab in the properties window, select 'Custom calculation script', and enter the following code: var str = this.documentFileName.split('.'); event.value = str[0];

4
Duplicate Across Pages

Right-click the configured text field and select 'Duplicate Across Pages' to make it appear in the exact same position on every page of the document.

Use a Custom JavaScript Calculation in a Text Field
Dynamic Updating: Because this field uses a calculation script, the displayed name will automatically update if you rename the PDF file and reopen it.
Efficient PDF Editing

Easily Add Headers and Footers with WPS Office

While JavaScript offers dynamic naming, WPS PDF provides an intuitive interface to quickly insert text, page numbers, and custom headers/footers across all pages in seconds without writing any code.

  1. 1. Open PDF: Launch WPS Office and open your PDF document.
  2. 2. Access Header/Footer: Go to the 'Insert' tab and click on 'Header and Footer'.
  3. 3. Customize Text: Enter your file name or desired text into the header or footer section.
  4. 4. Apply to All Pages: Set the page range to 'All Pages' and click 'OK' to apply the text throughout the document.
Easily insert custom headers and footers on all PDF pagesHighly compatible with Adobe Acrobat PDF formatsLightweight, fast, and user-friendly interfaceFree basic PDF editing and reading tools
microsoft office alternative - wps office

Frequently Asked Questions

Can I include the .pdf extension in the displayed file name?

Yes, you can modify the JavaScript to simply use `event.value = this.documentFileName;` to display the full file name including the extension.

Will the text field method update automatically if I rename the PDF?

Yes, because the script dynamically reads the document's file name property, the displayed name will update the next time the PDF is opened or calculated after renaming.

Do these JavaScript functions work in all PDF viewers?

Not all basic PDF readers or web browsers support custom JavaScript calculations. It is best viewed in robust editors like Adobe Acrobat or WPS PDF with advanced features enabled.

How do I change the font size of the automatically generated file name?

You can adjust the font size, style, and color in the 'Appearance' tab of the text field properties before duplicating it across pages.