This website contains various projects I have worked on. Currently, this includes JavaScripts for Adobe applications and general produciton tutorials using Adobe Creative Suite applications. All projects can be accessed from the right-hand menu.

image of a notepad
Adding a dynamic stamp to a PDF using Acrobat 7

Script Description

If you’re trying to add some text to a PDF, let’s say a header or footer you could use Acrobat’s built in “Headers and Footers” function. But, many times you will find that method comes up short. What if you want to apply the text across a hundred files? Or what if the text needs to change slightly each time? This tutorial describes one alternative to “Headers and Footers” for stamping text onto a PDF. It uses a batch process and JavaScript to get the job done.

Looking at the script

In this basic stamping script, I use the method “addWatermarkFromText” to add a stamp containing text to a PDF. I use this method three times to stamp the PDF in three different ways. The first method adds a single stamp to the first page of the document. The second method adds a stamp to every page in the document and the third method adds a stamp to every page on the document, where the stamp is slightly different on each page.

In the first part of the script, I add some comments to describe what the script is for, then I declare a couple of variables that will hold part of the contents of my stamp. I also declare a variable to hold the amount of pages in the PDF.

// ----- Basic PDF Stamping script ----- \\
/*
This script will place three stamps onto a PDF. The first stamp will only appear on the first page. The second
stamp will appear on all pages, and will not change. The third stamp appears on all pages and displays a number
that will increment.
*/

var myTopStamp = "Topstamp"; 
var myLowerStamp = "Lowerstamp";

var pageAmount = this.numPages; // Finds the length of the PDF

Next I add the first “addWatermarkFromText ” method using the “This” object, which describes the current active PDF. The “addWatermarkFromText ” method takes multiple parameters which are all defined in between ({ and });. The “cText” parameter holds the actuall text that you are using for the stamp. You can write the stamp here, in quotes or list a variable. In this example I have done both and joined them with the “+” operator. nStart and nEnd are the range of pages to apply the stamp to. nFontSize, as it suggest controls the size of the font in the stamp. The next three parameters align the stamp on the page. In this case I’m aligning the text to the left bottom portion of the page, and making the text left-justified. The last parameters “nHorizValue” and “nVertValue” position the stamp relative to it’s alignment in points.

// Stamping is achieved by using the "addWatermarkFromText" method
this.addWatermarkFromText({ 
cText: "Single page stamp " + myLowerStamp, 
nStart: 0,
nEnd: 0,
nFontSize: 8,
nTextAlign: app.constants.align.left,
nHorizAlign: app.constants.align.left,
nVertAlign: app.constants.align.bottom,
nHorizValue: 20, nVertValue: 20
});

Here I’m using the same method as before, but with some different values for the parameters. The main difference is the “nEnd” parameter is set to the “pageAmount” variable so that the stamp is applied to every page in the PDF. I have also changed the position of the stamp to the top right corner of the page. In the “cText” parameter I use the “\r” sequence to create a new line.

this.addWatermarkFromText({
cText: "Stamp with a line break" + myTopStamp + "\r" + "New line", 
nStart: 0,
nEnd: pageAmount, // Stamp on all pages untill the end of the PDF
nFontSize: 10,
nTextAlign: app.constants.align.right,
nHorizAlign: app.constants.align.right,
nVertAlign: app.constants.align.top,
nHorizValue: -20, nVertValue: -20
});

Here’s the third and final stamping method. This one is placed inside of a “for” loop. This is done so I can stamp page numbers onto the PDF, since the stamp contains the variable “i” which is incremented with each cycle of the loop. I also move the stamp to the left top corner of the PDF.

for (i = 0; i 	<= pageAmount; i ++)
{
	this.addWatermarkFromText({
	cText: "Dynamic stamp number: " + (i +1), 
	nStart: i,
	nEnd: i,
	nFontSize: 10,
	nTextAlign: app.constants.align.right,
	nHorizAlign: app.constants.align.left,
	nVertAlign: app.constants.align.top,
	nHorizValue: 20, nVertValue: -20
	});
}

Adding a new batch process

To run this script you will first have to create a new batch process within Acrobat. To do this click: Advanced → Batch Processing → New Sequence, and then name that sequence. With the new sequence highlighted click “edit sequence”. From the drop-down menu under item number two, select “Files Open in Acrobat”. Then click the “select commands” button and you will see a list of possible commands to add to your new sequence. Scroll down until you get to the JavaScript section and select the command “Execute JavaScript” and click “add”. Now with the new JavaScript command selected click “Edit”. A new window will appear this is the JavaScript editor. Paste the stamping script into this box and click “OK”. Keep clicking “OK” and close all windows.

Now the script is setup and ready for use. Open any PDF you want to try stamping and click: Advanced → Batch Processing. Select the new sequence you just created and click “Run Sequence”. Click “OK” on the next window to run the sequence on the open PDF and the script should execute.


back to top

Image of a notepad

Script table-of-contents

Script files


Other Projects:

Adobe JavaScripts ↑↓

Adobe Creative Suite Tutorials ↑↓



Rate the value of using JavaScript to stamp PDFs.

The only way to do it!
Not bad, but not that useful.
Why bother?