Dynamic Stamp Secrets

Thom ParkerFebruary 20, 2007

Was this tutorial useful to you?

Rating:

Log in to rate:
Print version of this tutorial

Scope: Acrobat 7.0 and later
Skill Level: Intermediate and Advanced
Prerequisites: Familiar with Acrobat JavaScript and Stamps

So you’ve finally figured out how to create a custom dynamic stamp for Acrobat. But after all that hard work, what seemed like days of slogging through molasses, it still doesn’t do what it’s supposed to and your boss is getting impatient. There are three things yet to be done. The stamp needs to display the filename of the document on which it is being placed, it needs to ask the user for some state information (i.e., approved, rejected and so on), and it needs to write this information and a date into the document metadata. This is crazy! Documentation on dynamic stamps is already hard enough to come by. But there’s nothing anywhere about how to do these tasks. Take heart! There is a solution.

Building a dynamic stamp

Before getting into the advanced details, let’s review some dynamic-stamp basics (full dynamic-stamp tutorial). A dynamic stamp is created by adding form fields to an existing stamp file. It doesn’t count if the fields are added to the original PDF that is turned into a stamp. Those fields are flattened out when the stamp file is created. They have to be added after the stamp is created. The stamp files live in two different folders on your hard drive. You can find out where these folder are by executing the following lines of code in the JavaScript Console:

app.getPath ("app", "stamps");

app.getPath ("user", "stamps");

Results shown below in Figure 1.

Figure 1 – Acrobat Stamp Folders
See larger image

A stamp file is a regular PDF in which Acrobat has added some special information. Acrobat automatically generates stamp files and gives them very cryptic names, so you’ll have to look through all of them to find the one that contains your stamps. Once you find it, you can change the filename to something more descriptive. This is a good practice to have anyway. Renaming your stamp files makes it easier to keep track of them.

To make the stamp dynamic, at least one of the added form fields must have a calculation script. When Acrobat displays the stamp on the menu or places the stamp on a document, it calls the calculation scripts on the stamp. If the calculation script changes the value of the field, then it is followed by a format event. The Validate and Keystroke events are not reliable, so don’t use them.

These events all occur in the context of the stamp file, i.e., the this object is the doc object of the stamp file. To demonstrate, place the following line of code in the calculation script for a field on your dynamic stamp:

event.value = this.documentFileName;

When you place the stamp on a PDF, it will display the name of the stamp file, not the name of the file on which the stamp is placed.

Limitations

There is only one serious limitation to a dynamic-stamp script. Stamp files do not execute JavaScript like a normal PDF file. For example, there are no document or page events. Variables and functions cannot be defined in a document script. The only scripts guaranteed to execute are the calculation scripts of the fields placed on the stamp. Everything necessary to execute this script must be present in the script.

Another, less-important limitation is that the event.rc value is meaningless in a dynamic stamp calculation script. It doesn’t block the field from acquiring the assigned value as it would in a normal calculation script.

Privileges

Acrobat grants dynamic stamp scripts a small amount of privilege. They are able to access the identity object, which is normally off limits to scripts in a PDF. The identity object contains the same user information listed in the Identity panel in Acrobat’s Preferences. However, dynamic stamp scripts are not fully privileged, so they cannot use other secure Acrobat properties and functions, i.e., anything in the Acrobat JavaScript Reference marked with a red (S) in the Rights Bar.

Figure 2 – Identity Object Entry from the Acrobat JavaScript Reference
See larger image

Dynamic scripts can call functions defined in a Folder-level script, so if you need some privileged code for your stamp, you can always place it in an external script file.

The secrets

Now, down to business. In the introduction, our intrepid PDF developer was directed to implement three seemingly impossible dynamic-stamp features.

  1. Display the name of the file (onto which the stamp is being placed) onto the stamp.
  2. Ask the user to choose some document state text to display on the stamp.
    (Approved, Rejected or In Process)
  3. Write the document state info and a date into the document metadata.

Features 1 and 3 require access to the document being stamped. We can’t use the this keyword because the dynamic-stamp script is in the context of the stamp file and we can’t use the app.activeDocs property because a document has to be disclosed before it shows up on the list. There are some other possible coding solutions, but they are all awkward and pose problems with version compatibility.

This issue seems impossible, but fortunately Acrobat JavaScript provides us with a solution. For dynamic-stamp scripts, the event.source property is an object containing several useful parameters, one of which is the Doc Object for the PDF being stamped, event.source.source. Use the following code to get the file name for the “source” PDF:

event.value = event.source.source.documentFileName;

To write a value into the document’s metadata, use this code:

var cDate = util.printd("mm/dd/yyyy", new Date());
event.source.source.info.StampInfo = "Rejected:" + cDate;

That takes care of issues 1 and 3. For feature 2, we can simply use app.response() to ask the user for a simple string. But there are a couple problems. The calculate script is called in two different situations: when it is shown on the menu, as well as when it is actually being placed on the document. We only want the popup to be displayed when the stamp is being placed on the document. To further complicate this, all calculation scripts on the stamp file are called at once, not just the ones on the particular stamp in use. So how do we tell the difference between these situations?

Again, event.source comes to the rescue. In this case, we use the event.source.forReal and event.source.stampName properties. The forReal property is true when the stamp is being placed on the document and false at all other times. The stampName property uniquely identifies the stamp in use. The trick for using the stampName property is getting the stamp’s name in the first place so we can write the script. Use the following code the first time you create the dynamic stamp:

event.value = event.source.stampName;
console.println("Stamp Name: " + event.source.stampName);

This short script displays the internal name of the dynamic stamp on the stamp, and in the JavaScript Console. Copy the name to the clipboard from the console to use in the final dynamic stamp script. For example:

if( event.source.forReal && (event.source.stampName == "#UdzyXagRctZoS5p43TZ43C")){
	event.value = app.response();
}

Putting it all together

Let’s soup up the code a little and lay it out for specific implementation. Our stamp needs two fields: one on which to place PDF file name and one on which to place the user entered value.

Calculation (Dynamic) Script for Document Name Field

event.value = event.source.source.documentFileName;

Calculation (Dynamic) Script for User Data Field
(this is also the same script that will set metadata)

var cAsk = "Enter One of: Approved, Rejected, or In Process ";
var cTitle = "Document State For Stamp ";
if(event.source.forReal && (event.source.stampName == "#UdzyXagRctZoS5p43TZ43C ")) {
	var cMsg = app.response(cAsk, cTitle);
	cMsg += " : " + util.printd( "mm/dd/yyyy ", new Date());
	event.value = cMsg; event.source.source.info.DocumentState = cMsg;
}

That’s all there is to it. Here’s what the stamp and popup response box look like:

Figure 3 – User input response box and resulting stamp

All code is presented in the example stamp file below, which will operate on either Acrobat 7 or 8. To use these stamps, place the file in one of Acrobat’s stamp folders. “Secrets Examples” should instantly appear on your Stamps menu.

Stamp Secrets Sample

Download [PDF: 91 KB]

Good Stamping!

Products covered:

Related topics:

Review & approve, Enhance PDF using JavaScript

Top Searches:

PDF files, Enterprise PDF, File compare, Online file sharing , Edit PDF, Create PDF, Interactive PDF, Action Wizard

15 comments

Thom Parker

1 week ago

Louis, the answer is simple. In JavaScript code, the opening and closing Parenthesis must be matched. The error you are seeing is being caused by a missing “Closing” parenthesis. Look through your code and make sure each opening “(” is matched with a “)” in the correct location.

louis

2 weeks ago

I am getting this syntax message when editing the script of my dynamic stamp in attempt to prompt user added data: “SyntaxError: missing )- in compound statement 6: at line 7” can you help me with this?

Lori Kassuba

1 month ago

Hi Javier,

You can create custom dynamic stamps in Acrobat 9, X and XI. Here is an updated tutorial on the subject:
http://acrobatusers.com/tutorials/creating-a-custom-dynamic-stamp-infographic

Thanks,
Lori

Thom Parker

1 month ago

Javier, You can do this on any version of Acrobat. Post the specific issue on the forum

Javier

1 month ago

I have tried doing this in Acrobat 9 and X.  Are creating these dynamic stamps disabled in these versions?

Thom Parker

11, 2013-03-05 05, 2013

Roy,
  The stamp name is part of the Template name in the stamp file. It’s not something I can explain here. However, I would suggest you create your stamp from scratch, you can give it your own name. Or learn more about stamps by purchasing the Stamp Book:
http://www.pdfscripting.com/public/All-About-PDF-Stamps-in-Acrobat-and-Paperless-Workflows-The-Book.cfm

Or becoming a member at www.pdfscripting.com

Roy

4, 2013-03-01 01, 2013

Thom,
I need your assistance, I was able to modify your example of the dynamic stamp secrets file, but I’not able to change the name of the stamp, the stamp reads “Example3”. How can I achieve this?

Thom Parker

11, 2012-10-01 01, 2012

Susan, what you are asking about is how to manipulate strings in JavaScript.  Here is an article on the topic, it includes and example of removing the extension from a file name:
http://acrobatusers.com/tutorials/splitting-and-rebuilding-strings

Susan

6, 2012-09-27 27, 2012

Thank you for a good tutorial.

Is there a way to get the following string to return the file name without the file type (.pdf)?
event.value = event.source.source.documentFileName;

Thom Parker

3, 2012-07-16 16, 2012

Julie,
  I’ve created many stamps that are very similar to what you’ve described, so it can definitely be done. The details are not something I could give you in this form. You’d need a training session. Or, If you are motivated, you’ll find all the details in this book:

http://www.pdfscripting.com/public/183.cfm

There are also tutorials and samples here:
http://www.pdfscripting.com - Watch the “Stamps Gone Wild” Video

We also provide custom development. Please feel free to contact me at support@windjack.com

Julie

12, 2012-07-11 11, 2012

Hello, I am trying to create a custom dynamic stamp which will stamp “PAID” followed by the date (which may or may not be the current date) and then the payment method (cheque, visa, mastercard, cash, debit, wire transfer). I managed to get the PAID part, but the rest is beyond me, seeing as I know nothing about JavaScript. I tried to at least get it to do the payment method part, based on the code you wrote for option 2, but it didn’t work. Any assistance would be much appreciated. :)

Thom Parker

12, 2012-07-09 09, 2012

Vernon,
  There are no examples here in the Comment area. However, date and time Dynamic stamps are this easiest to make. Adobe does not exactly provide a template, but they do provide several examples, i.e, all of the built-in stamps that display a date and time. There are other tutorials on this site on creating a dynamic stamp. This particular article is about creating advanced features.

If you want a better set of instructions for creating a basic date and time stamp then I would suggest purchasing this book.
http://www.pdfscripting.com/public/183.cfm

Vernon Alexander

11, 2012-07-03 03, 2012

I want to create a date and time stamp.  Is there a built in template to work from in the “Comment” area?  Am I missing something.

Thom Parker

7, 2012-06-26 26, 2012

Hello Mathew, I believe I already answered this question in the forums.

Mathew

12, 2012-06-25 25, 2012

Is there any way to build a dynamic stamp which can open an attached file (i.e. having the same function as the ‘attach file as comment’ tool)?

Leave a reply:

Fields marked with * are required.

Download
Acrobat XI trial

Get the trial now

Get expert advice.