Simple Acrobat Scripts I’ve Found to Be Useful

I create a lot of forms, and while Acrobat has some fantastic features, there are still quite a few things that I had to do manually. Below are a few of the small scripts I’ve put together to save myself a lot of time.

Version Check

This is a super simple script that simply detects what version of Acrobat Reader the user has and, if needed, recommends an upgrade. It’s a document level script so it will run as soon as the file is opened.

function VersionCheck(){
var v = app.viewerVersion;
if(v < 11){
app.alert("This version of Reader (v. " + v + ") will not be able to save the file. Please upgrade to the current version of Reader.");}}
VersionCheck();

This still allows the user to view and print the file. It’s simply a notification if some features won’t be available. It doesn’t save me a lot of time, but has been useful in encouraging end users to all be on the same version of Reader.

Set Default Values

This script simply takes the entered values and sets them as the default so if the form is reset, the values stay. It can be placed in a button or as part of an action.

for(var i = 0; i < this.numFields; i++){
var fname = this.getNthFieldName(i);
var f = this.getField(fname);
if(f == null){
continue;}
else{
try{f.defaultValue = f.value;}
catch(e){}}}

I use this when I’m filling out a lot of forms that have similar data. I can fill in all of the repeated information once, run the script, then just reset the form and do a ‘Save As.’ It has limited use, but it’s been really nice to have.

Set Field Properties

I have this set within an action so I can run it on any document I need. This goes through every field in the form and sets the properties to my company’s standards. It won’t catch everything, but it saves a lot of time on large forms.

var errorArray = [];
//Gather fields
for(var i = 0; i < this.numFields; i++){
var fname = this.getNthFieldName(i);
var f = this.getField(fname);
//Check for null fields
if(f == null){
errorArray.push(fname);
continue;}
//Set text field properties
if(f.type == "text"){
try{
//clear the tooltip
f.userName = "";
//limit text to field width
f.doNotScroll = true;
//Set font size
f.textSize = 9;
//Set test value (optional)
f.value = "September 22, 2022";}
//Note fields that didn't format
catch(e){
errorArray.push(fname);}}
//Set checkbox properties
else if(f.type == "checkbox"){
try{
f.userName = "";
f.textSize = 9;
//Set box to be marked with an 'x'
f.style = style.cr;
//Resize checkbox
var myRect = f.rect;
myRect[0] -= 1.5;
myRect[1] += 1.5;
myRect[2] += 1.5;
myRect[3] -= 1.5;
f.rect = myRect;}
//Note fields that didn't format
catch(e){
errorArray.push(fname);}}}
console.println(errorArray);

I use the ‘Auto-Detect Form Fields’ feature in Acrobat a lot. It’s a quick and easy way to get the majority of my fields in place. It won’t grab everything, but it will usually get about 80-90% of the fields I want. The problem is that the default fields don’t follow my company’s formatting standard, and it’s a pain to go through each field individually.

When working on this, I found a bug in Acrobat. Occasionally, the auto-detect will create a ‘ghost’ field, usually with checkboxes. The field is created, but it’s null and can’t be accessed. It caused me a lot of problems until try67 (a fantastic user on the Adobe forums) figured out what was going on.

Other Resources

I had to teach myself a lot about scripting in Acrobat, but there are some great sites available to help. Here are a few of the links that I found particularly helpful:

  • PDF Scripting
    A learning resource that also has sample script. (The dropdown calendar they have is very useful.)
  • Adobe Scripting Forum
    The official Adobe forum for Acrobat scripting. (This community is wonderful at helping debug code.) Also, make sure you get the Acrobat API, it has a lot of useful samples
  • Planet PDF
    Another learning resource that also has some sample scripts
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s