How to Add Automatic File Name Headers in PDF using Acrobat JavaScript
Question details
The user wants to use Acrobat JavaScript and batch processing to automatically insert the PDF file name (without the extension) into the header area of multiple document pages.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Automating the addition of custom multi-line headers to PDF files, specifically including the document's file name.
- Observed behavior
- The user needs to automatically generate left and right header text fields via script, populate them with the file name minus the .pdf extension, and then flatten the pages.
Ensure you have Adobe Acrobat Pro installed, as standard versions or free PDF readers do not support advanced JavaScript batch processing via the Action Wizard.
Create and Populate Header Fields using Acrobat JavaScript
Add multiline text fields to the header area, strip the file extension from the document name using JavaScript, and flatten the page.
To add headers containing the file name automatically, you can use the same approach as creating footers via JavaScript. By dynamically creating two distinct fields for the left and right header sections, you can position them precisely. Ensuring the 'multiline' property is enabled is essential if your header data requires multiple lines.
Open Acrobat Pro, navigate to the Tools menu, select 'Action Wizard', and click 'New Action' to start building your batch process.
Under the 'More Tools' section on the left, choose 'Execute JavaScript', add it to your action steps, and uncheck 'Prompt User'.
Click 'Specify Settings' and write your script to create text fields using 'this.addField()'. Set 'Field.multiline = true' for the created fields.
Use a JavaScript string replacement function like 'this.documentFileName.replace(/\.pdf$/i, "")' to extract the file name without the extension, and assign this value to the field.
End your script with 'this.flattenPages()' to embed the text fields permanently into the document, save the script, and run the batch action.

Easily Add File Names to PDF Headers with WPS Office
Skip the complex JavaScript coding. With WPS Office, you can batch add headers, footers, and file names to your PDF documents using an intuitive visual interface.
- 1. Open PDF in WPS: Launch WPS Office and open your PDF document.
- 2. Access Header & Footer Tool: Navigate to the 'Insert' tab on the top ribbon and select 'Header and Footer'.
- 3. Select Header Position: In the dialog box, click on the Left, Center, or Right header text box depending on where you want the file name.
- 4. Insert File Name Macro: Click the 'Insert Macro' dropdown below the text boxes and select 'File Name' to automatically populate the field.
- 5. Apply Settings: Click 'OK' to apply to the current file, or use the batch feature to apply this exact header configuration to multiple PDFs at once.

Frequently Asked Questions
Why is the .pdf extension showing in my Acrobat header?
If the .pdf extension is visible, your JavaScript needs a string replacement function to strip it. Ensure you are using a snippet like `.replace(/\.pdf$/i, "")` on the `documentFileName` property before assigning it to the header field.
How do I make the header text wrap to a second line in Acrobat?
When creating the text field via JavaScript, you must explicitly set the multiline property to true (e.g., `myField.multiline = true;`). This allows the text to flow onto multiple lines within the field's designated boundary coordinates.
Do I need to flatten the PDF after adding a JavaScript header?
Yes. Flattening the PDF pages using `this.flattenPages()` after adding the text fields converts the interactive form fields into permanent static text. This ensures the header prints correctly and prevents the fields from being accidentally edited or deleted.




