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

Add watermark with file name and page numbers using JavaScript

Community Beginner ,
Apr 09, 2020 Apr 09, 2020

Copy link to clipboard

Copied

Hi. I'm trying to write a script for incorporation into a custom Action that can run on a batch of files. The goal is to add a watermark in the upper right-hand corner of all pages with:

  • Line 1: File ID = [file name without extension]
  • Line 2: Page # of 30

 

For example, for a pdf named 'MyFile.pdf' that contains 30 pages, the script would add the following text in the upper left corner of each page:

 

File ID = MyFile

Page # of 30

 

where # starts at 1 and increments by 1 on each subsequent page

 

See draft script below. As written, it only adds the watermark to the first page of the pdf. I've successfully created scripts to do each line of the watermark independently, but can't seem to figure out how to accomplish both into a single script. Any suggestions are very appreciated!

 

for (var p = 0; p < this.pageNum; p++);
var cMyText = "File ID = " + event.target.documentFileName.replace(/\.pdf$/i,"") + "\nPage " + (p+1) + " of " + numPages;
event.target.addWatermarkFromText({
	cText: cMyText,
	nFontSize:10,
	aColor: color.red,
	nHorizAlign:app.constants.align.left,
	nHorizValue: 36,
	nVertAlign:app.constants.align.top,
	nVertValue: -36,
	nStart: p
});

 

TOPICS
Acrobat SDK and JavaScript

Views

3.6K

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Beginner , Apr 11, 2020 Apr 11, 2020

Here is another solution using a form field instead of a watermark. This works much faster and is a better solution for reasons described by @Thom_Parker. Thanks to Kas Thomas for the starter script.

 

/* Add File Name and Page Number */
var inch = 72;
for (var p = 0; p < this.numPages; p++) { 
 // put a rectangle at .25 inch, .25 inch 
  var aRect = this.getPageBox( {nPage: p} ); 
  aRect[0] += .25*inch;// from upper left corner of page 
  aRect[2] = aRect[0]+8*inch; // Make it 8 inch wide 
  aRe
...

Votes

Translate

Translate
Community Expert ,
Apr 09, 2020 Apr 09, 2020

Copy link to clipboard

Copied

Your for loop ends at the current page. For all pages you must use this.numPages

Votes

Translate

Translate

Report

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 10, 2020 Apr 10, 2020

Copy link to clipboard

Copied

Thanks @Bernd_Alheit. These were the clues I needed to solve the puzzle.

Votes

Translate

Translate

Report

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 ,
Jul 19, 2022 Jul 19, 2022

Copy link to clipboard

Copied

Qould this mean that the final code is 

for (var p = 0; p < this.numPages; p++);
var cMyText = "File ID = " + event.target.documentFileName.replace(/\.pdf$/i,"") + "\nPage " + (p+1) + " of " + numPages;
event.target.addWatermarkFromText({
	cText: cMyText,
	nFontSize:10,
	aColor: color.red,
	nHorizAlign:app.constants.align.left,
	nHorizValue: 36,
	nVertAlign:app.constants.align.top,
	nVertValue: -36,
	nStart: p
});

I'm looking to essentially do the same thing with very slightly different settings but I suck at java (at coding in general too)

Votes

Translate

Translate

Report

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 ,
Jul 20, 2022 Jul 20, 2022

Copy link to clipboard

Copied

Replace the semi-colon at the end of the first line with an opening curly bracket ( { ) and add a closing one  ( } ) at the end of the code.

Votes

Translate

Translate

Report

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 ,
Jul 21, 2022 Jul 21, 2022

Copy link to clipboard

Copied

Thanks! Works like a charm, obviously I suck at that stuff but now I've modified it so that it does barcode (using a barcode font) and it's great!

Votes

Translate

Translate

Report

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 10, 2020 Apr 10, 2020

Copy link to clipboard

Copied

The "Stamp and Number Document" and/or the "File Name Stamper" actions were made for you:

https://acrobatusers.com/actions-exchange/

Votes

Translate

Translate

Report

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 10, 2020 Apr 10, 2020

Copy link to clipboard

Copied

Thanks @JR_Boulay. I appreciate you pointing me to these tools, which are fantastic, adaptable for a wide range of needs, and are vastly more sophisticated than the script I was trying to write. For my specific use case though, I wanted to accomplish both in a single script and have it work on a batch of files without having to respond to a popup dialog for each file operated on by the Action. And my JS skills are not nearly advanced enough for me to modify those premade actions.

Votes

Translate

Translate

Report

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 11, 2020 Apr 11, 2020

Copy link to clipboard

Copied

You should be able to edit these actions, you can copy-paste the script from one to the other, and add a "tool" for Saving.

 

editAction.png

Votes

Translate

Translate

Report

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 11, 2020 Apr 11, 2020

Copy link to clipboard

Copied

I see. Thank you for illustrating this.

Votes

Translate

Translate

Report

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 10, 2020 Apr 10, 2020

Copy link to clipboard

Copied

So I agree with JR. Use the free actions that already exist. You can modify the scripts to make them work the way you want.

 

But if you want to do this yourself, why are use a watermark?  The watermark is a rather top heavy way to do this. It has an issue your code doesn't deal with. A watermark creates an OCG layer. When you create the watermark on the next page, you replace/overwrite the last watermark OCG. A PDF file can have only one official watermark. You can read about it here. 

https://acrobatusers.com/tutorials/create_use_layers/

 

A much easier way to accomplish the same task is to use a form field. At the end the script can flatten the fields if you want them pushed into the page content, or the field can be set to readonly. 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

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 10, 2020 Apr 10, 2020

Copy link to clipboard

Copied

Thanks @Thom_Parker. I see that you authored both of those Actions, as well as the tutorial that led me down the watermark route originally. Awesome! My JS skills are very amatuerish right now and I'm not yet ready to take on modifying your very impressive scripts. However, I see what you're saying about having multiple watermarks and will look into the form field approach. I did notice that my script runs very slowly. Do you know if using a form field would be faster? And is this example considered a form field?

Votes

Translate

Translate

Report

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 11, 2020 Apr 11, 2020

Copy link to clipboard

Copied

Yes, that script uses a form field. And I'm sur it would be faster then watermarking.

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

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 11, 2020 Apr 11, 2020

Copy link to clipboard

Copied

Great, appreciate the response. I'll look into that method and will post an alternative solution when I get there.

Votes

Translate

Translate

Report

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 11, 2020 Apr 11, 2020

Copy link to clipboard

Copied

Here is another solution using a form field instead of a watermark. This works much faster and is a better solution for reasons described by @Thom_Parker. Thanks to Kas Thomas for the starter script.

 

/* Add File Name and Page Number */
var inch = 72;
for (var p = 0; p < this.numPages; p++) { 
 // put a rectangle at .25 inch, .25 inch 
  var aRect = this.getPageBox( {nPage: p} ); 
  aRect[0] += .25*inch;// from upper left corner of page 
  aRect[2] = aRect[0]+8*inch; // Make it 8 inch wide 
  aRect[1] -= .25*inch; 
  aRect[3] = aRect[1] - .5*inch; // and .5 inch high 
  var f = this.addField("p."+p, "text", p, aRect ); 
  f.textSize = 10;  // 10-pt type
  f.textColor = color.red; // use whatever color you want
  f.strokeColor = color.white; 
  f.textFont = font.Helv;
  f.multiline = true; // Make it multiline
  f.value = "File ID = " + this.documentFileName.replace(/\.pdf$/i,"") + "\nPage " + String(p+1) + " of " + this.numPages;  // page numbering is zero-based
  f.readonly = true;
} 

 

Votes

Translate

Translate

Report

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 ,
Dec 08, 2023 Dec 08, 2023

Copy link to clipboard

Copied

LATEST

Hi gents. I know this post is a bit old, but I'm hoping you could help me add a line to this script to display/print the page label for each page in the PDF document. My scripting is amature at best as well, so any point in the right direction would be apprectiated. Thanks.  

Votes

Translate

Translate

Report

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