Adding dynamic stamps to PDFs when printing

This JavaScript sample allows for the addition of time and date stamps, expiry dates or other information to give you even greater control of your sensitive PDF documents. Specifically, it creates a text form field on the fly. This field is can then be filled with information at the time of printing time. The font style, size, position etc. can all be customized as well.

Additionally, the code sets a JavaScript to be executed when the document is printed. In the example, there is only a simple message, but it could theoretically be edited to display anything the coder wants (e.g. metadata, user info, confidentiality notes, database query responses etc.)

In order to execute the script, the user will need either Acrobat 5 or the Professional versions of Acrobats 6 or 7. Another neat application of the script is that it can be run across multiple files using Acrobat’s batch processor. Once applied, the chosen text will be displayed regardless of whether the document is printed from the full version of Acrobat or the free Adobe Reader.

This script is a great tool for content creators who want to keep a ‘lock-down’ on printed material. This originally displayed ‘Document is valid for 7 days after printing’ and specified the expiry date and time. That’s only one of the many options however; any collected information can be printed in this way. For instance, it could include copyright information or even communicate with a database for dynamic stamping. The possibilities are endless!

Another advantage is that the script allows dynamic content to be added to a PDF in the free Reader — even adding a DRAFT or CONFIDENTIAL message would otherwise require the full Acrobat product.

Although I originally wrote this to be executed via a custom tool button using ARTS PDF Aerialist, you can also execute it from Acrobat Professional’s JavaScript debugger.

// Author: Sean Stewart, ARTS PDF
// Date: 19 Jan 2005
// Description: This script adds a fillable text field to bottom of all
// pages. It also sets a doc level JavaScript that executes when the
// document is printed and fills the field with date at print time. Can
// be customized to display any information required.

// Add field to each page of form
var inch = 72;

for (var p = 0; p < this.numPages; p++) {
// create rectangle quads for field
var aRect = this.getPageBox( {nPage: p} );
aRect[1] = .5*inch;
aRect[3] = aRect[1] - 24; // and 24 points high

// now construct text field to fill with date information
var f = this.addField('PrintField', 'text', p, aRect )
f.delay = true;
f.textSize = 16;
f.textFont = font.HelvB;
f.textColor =;
f.alignment = 'center';
f.readonly = true;
f.print = true;
f.hidden = true;
f.delay = false;

// Adds a doc level script to populate text field when doc prints from
// Acrobat or the free Adobe Reader. Note: the 'r' character represents a
// carriage return.

var myWillPrintScript = 'var f = this.getField('PrintField'); r'
+ 'f.hidden = false; r'
+ 'var d = new Date(); r'
+ 'var year = d.getYear()+1900; r'
+ 'var mins = d.getMinutes(); r'
+ 'if (mins < 10) { r'
+ ' mins = '0' + mins}; r'
+ 'if (d.getHours() >= 12) { r'
+ 'mins = mins + ' PM'; r'
+ '} else { r'
+ 'mins = mins + ' AM'; } r'
+ (d.getMonth()+1) + '/' + d.getDate() + '/'
+ year + ' ' + d.getHours() + ':' + mins; r'

var myDidPrintScript = 'var f = this.getField('PrintField'); r'
+ 'f.value = ''; r'
+ 'f.hidden = true; r'

// Now set the scripts to execute on the Will/Did Print events.

this.setAction('WillPrint', myWillPrintScript);
this.setAction('DidPrint', myDidPrintScript);

You May Also Like

About the Author: Sean Stewart

Leave a Reply