PDF Editing Issues

How to Add Automatic File Name Headers in PDF using Acrobat JavaScript

Aamir Naveed AkramAamir Naveed Akram Nov 2, 2026 869 views

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.

How to Add Automatic File Name Headers in PDF using Acrobat JavaScript
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.
Before you start

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.

Solution 1Recommended

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.

1
Access Action Wizard

Open Acrobat Pro, navigate to the Tools menu, select 'Action Wizard', and click 'New Action' to start building your batch process.

2
Add JavaScript Step

Under the 'More Tools' section on the left, choose 'Execute JavaScript', add it to your action steps, and uncheck 'Prompt User'.

3
Write the Field Creation Script

Click 'Specify Settings' and write your script to create text fields using 'this.addField()'. Set 'Field.multiline = true' for the created fields.

4
Extract and Assign File Name

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.

5
Flatten the Pages

End your script with 'this.flattenPages()' to embed the text fields permanently into the document, save the script, and run the batch action.

Create and Populate Header Fields using Acrobat JavaScript
Adjusting Page Coordinates: You will need to calculate and adjust the field positioning coordinates dynamically in your script if your PDF batch contains pages of varying sizes.
Native PDF Editing Tool

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. 1. Open PDF in WPS: Launch WPS Office and open your PDF document.
  2. 2. Access Header & Footer Tool: Navigate to the 'Insert' tab on the top ribbon and select 'Header and Footer'.
  3. 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. 4. Insert File Name Macro: Click the 'Insert Macro' dropdown below the text boxes and select 'File Name' to automatically populate the field.
  5. 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.
Visual Header & Footer editor with no coding requiredBatch process multiple PDFs simultaneously with one clickHighly compatible with standard Adobe PDF formatsLightweight and fast alternative for daily PDF document tasks
microsoft office alternative - wps office

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.