Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Batch add header text based on file name

New Here ,
Dec 02, 2022 Dec 02, 2022

I have a large number of files. Is it possible to automate adding a header to the upper right side of the page based on their file names? Thanks!

TOPICS
Create PDFs , Edit and convert PDFs , JavaScript , Print and prepress , Standards and accessibility
5.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Dec 31, 2022 Dec 31, 2022

Hi,

You can create an action wizard with the script below or use the one attached into the pdf file.

this.addWatermarkFromText({
	cText: this.documentFileName, // The text to use as the watermark
	nTextAlign: app.constants.align.left, // (optional) The text alignment to use for cText within the watermark
	cFont: font.HelvB, // (optional) The font to be used for this watermark
	nFontSize: 10, // (optional) The point size of the font to use for the watermark
	aColor: color.black, // (optional) The color to use for the watermark
	nStart: 0, // (optional) The 0-based index of the first page
	nEnd: this.numPages-1, // (optional) The last page
	bOnTop: true, // (optional) A Boolean value specifying the z-ordering of the watermark
	bOnScreen: true, // (optional) A Boolean value to indicate whether the watermark should be displayed when viewing the document on screen
	bOnPrint: true, // (optional) A Boolean value to indicate whether the watermark should be displayed when printing the document
	nHorizAlign: app.constants.align.left, // Al(optional) A number indicating how the watermark should be aligned horizontally
	nVertAlign: app.constants.align.top, // (optional) A number indicating how the watermark should be aligned vertically
	nHorizValue: 15, // (optional) A number used to shift the horizontal position of the watermark on the page
	nVertValue: -15, // (optional) A number used to shift the vertical position of the watermark on the page
	bPercentage: false, // (optional) A Boolean value used to indicate whether nHorizValue and nVertValue represent a percentage of the page size or an explicit number of points
	nScale: 1.0, // (optional) The scale to be used for the watermark
	bFixedPrint: false, // (optional) A Boolean value that indicates that the watermark should be added as a FixedPrint Watermark annotation
	nRotation: 0, // (optional) The number of degrees to rotate the watermark counterclockwise
	nOpacity: 1 // (optional) The opacity to be used for the watermark
});
this.saveAs(this.path);

Then you run it and you add files or folder before starting.

Capture d’écran 2022-12-31 à 14.18.18.pngexpand image

@+

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Dec 31, 2022 Dec 31, 2022

Hi xcd.a,

 

Thank you for reaching out.

 

Please try the Action Wizard tool in Acrobat to add the header to the file. For more information on Acrobat Header, please refer to the following help page: https://helpx.adobe.com/acrobat/using/action-wizard-acrobat-pro.html.

Check if that works for you.

 

Thanks,

Meenakshi

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 31, 2022 Dec 31, 2022

Hi,

You can create an action wizard with the script below or use the one attached into the pdf file.

this.addWatermarkFromText({
	cText: this.documentFileName, // The text to use as the watermark
	nTextAlign: app.constants.align.left, // (optional) The text alignment to use for cText within the watermark
	cFont: font.HelvB, // (optional) The font to be used for this watermark
	nFontSize: 10, // (optional) The point size of the font to use for the watermark
	aColor: color.black, // (optional) The color to use for the watermark
	nStart: 0, // (optional) The 0-based index of the first page
	nEnd: this.numPages-1, // (optional) The last page
	bOnTop: true, // (optional) A Boolean value specifying the z-ordering of the watermark
	bOnScreen: true, // (optional) A Boolean value to indicate whether the watermark should be displayed when viewing the document on screen
	bOnPrint: true, // (optional) A Boolean value to indicate whether the watermark should be displayed when printing the document
	nHorizAlign: app.constants.align.left, // Al(optional) A number indicating how the watermark should be aligned horizontally
	nVertAlign: app.constants.align.top, // (optional) A number indicating how the watermark should be aligned vertically
	nHorizValue: 15, // (optional) A number used to shift the horizontal position of the watermark on the page
	nVertValue: -15, // (optional) A number used to shift the vertical position of the watermark on the page
	bPercentage: false, // (optional) A Boolean value used to indicate whether nHorizValue and nVertValue represent a percentage of the page size or an explicit number of points
	nScale: 1.0, // (optional) The scale to be used for the watermark
	bFixedPrint: false, // (optional) A Boolean value that indicates that the watermark should be added as a FixedPrint Watermark annotation
	nRotation: 0, // (optional) The number of degrees to rotate the watermark counterclockwise
	nOpacity: 1 // (optional) The opacity to be used for the watermark
});
this.saveAs(this.path);

Then you run it and you add files or folder before starting.

Capture d’écran 2022-12-31 à 14.18.18.pngexpand image

@+

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 13, 2024 Feb 13, 2024

This worked for me. Although, the text was very thin and small. I changed the font to 14 points and bold, then revised the script by deleting "left" and replacing it with "center". Right was too far right and outside the printing area. FYI

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 20, 2024 Apr 20, 2024

@bebarth 

Thank you for your script. It works perfect. Could you please help me by changing the font. I want to use the font "Segoe UI". I was not able to do it.

Additionally, do you know how it is possible, to read only a part of the file name and add this text to the watermark?

All my file names look like this (numbers and text of course differs from this example below): 

01 Text1.pdf

02 Text2.pdf

03 Text3.pdf

XY Text4.pdf

XYZ Text5.pdf

........

Would it be possible to write only the numbers/text which is in front of the space or after the text after the space? If the space is a problem, I could use the "-" sign as well, in case this helps. The final result should look like this:

01

02

03

XY

XYZ

or the text after the space

Text1

Text2

Text3

Text4

Text4


Thank you for any help. 

Best Regards, Ischni

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 20, 2024 Apr 20, 2024

Hi,
To use Segoe UI font, you must write:

...
cFont: SegoeUI,
...

 For the text, you can split the file name without the .pdf extension in a table with:

var bothTexts=this.documentFileName.substr(0,this.documentFileName.length-4).split(" ");

then you will use the first element of the table bothTexts[0] if you want to use the number or the second one bothTexts[1] if you prefer the text.

...
cText: bothTexts[0], // or bothTexts[1]
...

@+

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 20, 2024 Apr 20, 2024

Thank you for your quick answer. Both does not work for me. Would you mind to insert it into the code which you have posted above on Dec 31, 2022? Many Thanks.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 20, 2024 Apr 20, 2024

Yes, of course!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 22, 2024 Apr 22, 2024

I have solved it:

function splitFileName() {
    var fileName = this.documentFileName.replace('.pdf', '');
    var words = fileName.split(' '); 
    var topText = words[0];
    var bottomText = words.slice(1).join(' ');
    return [topText, bottomText];
}
 
function addWatermarks() {
    var words = splitFileName.call(this);
    var topText = words[0]; 
    var bottomText = words[1];
 
    this.addWatermarkFromText({
        cText: topText,
        nTextAlign: app.constants.align.center,
        cFont: "Segoe UI",
        nFontSize: 10,
        aColor: color.black,
        nHorizAlign: app.constants.align.right,
        nVertAlign: app.constants.align.top,
nHorizValue: -50,
        nVertValue: -30,
        bOnTop: true,
        bOnScreen: true,
        bOnPrint: true,
        nRotation: 0
    });
 
    this.addWatermarkFromText({
        cText: bottomText, 
        nTextAlign: app.constants.align.center,
        cFont: "Segoe UI",
        nFontSize: 10,
        aColor: color.black,
        nHorizAlign: app.constants.align.right,
        nVertAlign: app.constants.align.top,
nHorizValue: -50,
        nVertValue: -50,
        bOnTop: true,
        bOnScreen: true,
        bOnPrint: true,
        nRotation: 0
    });
}
 
addWatermarks.call(this);

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 13, 2025 Jan 13, 2025

Act like I have literally no experience with coding, but have thousands of documents that need to be branded with their file names minus .pdf. Most files are standard letter page size and I need the output to be Font - Arial Bold, Size - 12, Font Colour - Black, Top Margin - .5, Bottom Margin - .5, Left Margin - 1, Right Margin - .4 in the right header. Again, no experience with coding, can't even find where to enter the script above.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 14, 2025 Jan 14, 2025

Hi,

Do you want the file name on each page or only on the first one?

I guess dimensions you indicate are in inches...

Could you post a sketch of the position we need to place the file name because I am not sure to understand!

@+

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 14, 2025 Jan 14, 2025

Hello,

File name on each page, margins are in inches. In total there are 3 sections of branding for these files, the others are batchable (page number on bottom right and source info on bottom centre), because the top right is unique based on file name I haven't found a way to batch it. The below image is a page letter sized (8.5x11 inches) and shows the position of the top right file name header.

 

Kaitlyn230988035eh7_0-1736869272944.pngexpand image

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 14, 2025 Jan 14, 2025

Hi,

Here is the modified script to reach your request.

this.addWatermarkFromText({
	cText: this.documentFileName.replace(/.pdf$/i,""), // The text to use as the watermark
	nTextAlign: app.constants.align.right, // (optional) The text alignment to use for cText within the watermark
	cFont: "Arial,Bold", // (optional) The font to be used for this watermark
	nFontSize: 12, // (optional) The point size of the font to use for the watermark
	aColor: color.black, // (optional) The color to use for the watermark
	nStart: 0, // (optional) The 0-based index of the first page
	nEnd: this.numPages-1, // (optional) The last page
	bOnTop: true, // (optional) A Boolean value specifying the z-ordering of the watermark
	bOnScreen: true, // (optional) A Boolean value to indicate whether the watermark should be displayed when viewing the document on screen
	bOnPrint: true, // (optional) A Boolean value to indicate whether the watermark should be displayed when printing the document
	nHorizAlign: app.constants.align.right, // Al(optional) A number indicating how the watermark should be aligned horizontally
	nVertAlign: app.constants.align.top, // (optional) A number indicating how the watermark should be aligned vertically
	nHorizValue: -.4*72, // (optional) A number used to shift the horizontal position of the watermark on the page
	nVertValue: -.5*72, // (optional) A number used to shift the vertical position of the watermark on the page
	bPercentage: false, // (optional) A Boolean value used to indicate whether nHorizValue and nVertValue represent a percentage of the page size or an explicit number of points
	nScale: 1.0, // (optional) The scale to be used for the watermark
	bFixedPrint: false, // (optional) A Boolean value that indicates that the watermark should be added as a FixedPrint Watermark annotation
	nRotation: 0, // (optional) The number of degrees to rotate the watermark counterclockwise
	nOpacity: 1 // (optional) The opacity to be used for the watermark
});
// To keep the same file name
//this.saveAs(this.path);
// To modify the  file name
this.saveAs(this.path.replace(/.pdf$/i," (with Header).pdf"));

The action wizard is included into the attached file.

Capture d’écran 2025-01-15 à 08.39.20.pngexpand image

Please read this post to know how use it for several files in a same time. In a first step, try with just a few files instead of all your thouthands ones.

With this script, your files are renamed " (with Header)". If you wish to keep the original file name, use the line:

this.saveAs(this.path);

at the end of the script (but I suggest using a copy of your files).

Let me know.

@+

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 06, 2025 Mar 06, 2025

This is gold!! Thank you so much for this script. To avoid having to go back and add page numbers to the files, I was hoping to add to the script to include "Page # of #" after the FileName, 

+ "\nPage " + (p+1) + " of " + numPages;

but when I tried to insert the above after the , but before the // 

cText: this.documentFileName.replace(/.pdf$/i,""), // 

unfortunately, this didn't work, so I'm guessing it's expecting a different "name" for the page numbering function? I've only done rudimentary coding, usually based on a generous soul's (such as yourself) file so am sure I've mixed scripting languages.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 07, 2025 Mar 07, 2025

Hi,

Try that:

for (var p=0; p<this.numPages; p++) {
	this.addWatermarkFromText({
		cText: this.documentFileName.replace(/.pdf$/i,"\nPage "+(p+1)+" of "+this.numPages),
		nTextAlign: app.constants.align.right,
		cFont: "Arial,Bold",
		nFontSize: 12,
		aColor: color.black,
		nStart: p,
		bOnTop: true,
		bOnScreen: true,
		bOnPrint: true,
		nHorizAlign: app.constants.align.right,
		nVertAlign: app.constants.align.top,
		nHorizValue: -.4*72, 
		nVertValue: -.5*72,
		bPercentage: false,
		nScale: 1.0,
		bFixedPrint: false,
		nRotation: 0,
		nOpacity: 1 
	});
}
this.saveAs(this.path.replace(/.pdf$/i," (with Header).pdf"));

@+

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 07, 2025 Mar 07, 2025

TYSM! If you will allow me to pick your brain one more time, is there a way to shrink the document to accommodate the watermark like the appearance options in Header/Footer or Bates Numbering? 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 07, 2025 Mar 07, 2025
LATEST

Not sure to understand your request!
What do you mean exactly?

@+

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 24, 2024 Apr 24, 2024

Hi, Can you elaborate on how to apply this to multiple open files?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 24, 2024 Apr 24, 2024

It's explained above, you create an action wizard and you add files...

@+

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines