How to Automatically Display PDF File Name on All Pages
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.

- Product
- 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.
Ensure you have a PDF editor installed that supports form fields and custom JavaScript execution, or advanced header and footer customization.
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.
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.
Open the text field properties and check the 'Read-Only' option so the text cannot be manually altered by viewers.
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];
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.

Utilize a Batch File Name Stamper Action
Use specialized batch processing actions to stamp the active file name across multiple documents.
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. Open PDF: Launch WPS Office and open your PDF document.
- 2. Access Header/Footer: Go to the 'Insert' tab and click on 'Header and Footer'.
- 3. Customize Text: Enter your file name or desired text into the header or footer section.
- 4. Apply to All Pages: Set the page range to 'All Pages' and click 'OK' to apply the text throughout the document.

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.




