Home » PDF Forms, PDF JavaScript

PDFScripting.com Demonstrates How To Copy PDF Scripts

20 Jan 2009 | | 5 Comments

WindjackEditors note: I’d like to start out by giving Thom Parker, CEO of WindJack Solutions a big thank you for the tips that he is contributing to our site — he has been developing world-class plug-ins for Adobe-related products since 1997 (particular Acrobat).  However, in recent times, he’s launched a website called pdfscripting.com (requires payment) which has a very specific focus on making PDF Scripting accessible to any from the novice to the expert.

One of the particular areas of pdfscripting.com of which I’m a fan, is the Copy-n-Paste Script section. Based on this, I’ve asked Thom to give us here at 4xPDF, the occasional tip, script and example to get you going with PDF Scripting.

Today Thom steps us through how to copy pdf scripts….

How to Copy PDF Scripts

By Thom Parker, www.pdfscripting.com (For Acrobat/PDF forms)

In a PDF file, and this is doubly true for LiveCycle forms, all the fancy features (and some of the ordinary ones) are implemented with custom scripts. The most popular way to acquire these fancy features for your own PDFs is to copy a script from another PDF.  If you see a PDF with something you like, or someone publishes a script in a forum or an article, you just copy the script, paste it into your PDF, and you’re off to the races.  No pain, no sweat, just instant success 🙂

Unhappily, it really doesn’t work that way 🙁

Half the questions I answer on the forums are failed attempts to copy functionality from another source.   Unless you are properly equipped, trying to copy someone else’s code can result in a great deal of pain, suffering (mostly for the other people in the office), and occasionally, the destruction of company property.

But don’t lose hope.  Copying scripts is still a viable way to implement functionality that’s far beyond your current scripting knowledge.  To make it work you only need to have a few basic tools at hand.  Listen closely and I will provide you with the keys to ultimate success, promotions, riches, celebrity marriages, and all the other grand rewards of electronic documentation.

The Essentials for Copying a PDF Script

The scripting language for Acrobat is JavaScript.  However, just any old JavaScript won’t do.  JavaScript is a programming language specifically designed to be used as an application’s scripting language.  As such, each application that uses it has its own special version.  JavaScript for a Web page has absolutely nothing to do with Acrobat and PDF.  So don’t even think about trying to use a script that comes from anything other than a PDF file or an article on Acrobat JavaScript.

Along these same lines, Acrobat supports two different form technologies- traditional AcroForms (since Acrobat 3), and LiveCycle (XFA) forms (since Acrobat 7).  The scripting models for these technologies are so different from one another it’s like they were developed by two completely different companies for two completely different applications, and in two completely different countries.  And in fact, they were.   Trying to use a script from one technology in the other is like trying to get your dog to use the kitty litter box.   You might get it to work, but it’s a mess.  Always make sure you’re copying scripts from the right technology.

The next thing you have to understand about scripts is that they are usually written for a specific PDF.  This means that you will, in all likelihood, have to modify the script in some way.  This is the source of most failures. PDF scripts will almost always depend on a certain document setup, or on certain document elements being present. Form fields are the most common dependency, but there are others such as attachments, bookmarks, and disclosure to name a few.  You’ll know that an AcroForm script requires a form field if it has any code that looks like this:

this.getField(“MyField”)

In this case you have two choices.  Change “MyField” to the name of the equivalent field on your form or change the name of your field to “MyField.”  There are other ways to reference form fields in scripts.  So if you see something you think might be a field name but aren’t sure, then you’re going to have to get your hands a little dirty.

I know that many of you are going to find this next task onerous and repugnant, but like cleaning out that litter box, it’s just something you have to do every so often.  I am of course talking about using the Acrobat JavaScript Reference.  You don’t have to understand the reference.  All you have to do is look up the name of the thing that looks like it might reference a custom document feature you’re going to need to change.  And then read the text.  This should give you enough info to decide if, and what, changes may be needed.

Another thing about modifying scripts is that programming languages (not just JavaScript but all programming languages) are notoriously finicky about minor, seemingly insignificant details.  Have a comma in the wrong place, forget to capitalize a name, or don’t end a line in a semi-colon and you could inadvertently bring down the corporate network.  You’d think Acrobat could kind of figure out what you really meant to do with the script and be nice about it, but it doesn’t work that way.  A language interpreter is a bit like a sadistic master (which might explain a bit about programmers), so keep your eye on the details. Don’t make partial copies of scripts or make changes you don’t understand.

If you are going to make modifications, it’s a good idea to get into the correct mindset.  Programmers are obsessed with obscure symbols and non-intuitive structures, i.e., languages are specifically designed to be as unintelligible as possible.  This is a job security thing so it’s not going to be getting any easier any time soon.  But there are some little tidbits that can help.  For example, anything that you would normally think starts with “1” like page numbers, starts with a “0” in a script.  Proper names of things will almost always appear in double quotes, and not just any quotes.  They have to be plain text quote marks.  If you copied a script from a word processing program like Word, you might be in trouble.  Look at the script in Acrobat’s JavaScript editor, if the original quotes have been replaced by funny looking characters you’ll have to replace them all with real quote marks, and make sure you have both quotes there.

Finally, it’s rare that a script is just this single thing that’s off by itself in its own world.  A PDF is a very diverse environment and contains a large number of places where a script can be used.  It’s quite normal to have scripts that depend on other scripts, and this is especially true for forms.   So it’s important that when copying a script from another PDF that you identify all of the scripts that need to be copied, and their correct locations inside the PDF.  Here’s a link to an article that will help:

http://www.acrobatusers.com/tutorials/2006/where_js_goes

Probably the most common situation is a script that uses functions, variables and data that are defined in a global area.  In an AcroForm the global area is called a Document Level Script.   In a LiveCycle form it’s not so definite.  The Pseudo-Global area is called a Scripting Object, and one of these puppies can be attached to practically any object in the form.

Now you’re ready to do some power script copying.  Good luck and don’t get discouraged.  Remember all the cool stuff that will be coming your way once you get this down.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

5 Comments »

  • Vijay said:

    Thanks for the clarifications, they explain a lot of painful experiences!

  • Bill said:

    That http://www.pdfscripting.com is not a free site. It requires payment in order to download all pdf related stuff. This is all about money, nothing else.

  • Rowan Hanna said:

    Hi Bill,

    I’m not sure what the problem here is — unless I’m mistaken, I don’t think we indicated that PDFScripting.com was a free site? Thom is one of foremost experts in JavaScript programming for PDF, so the money is well spent if you want to learn all the ins-and-outs of how to get the most out of JavaScript in PDF.

    Rowan

  • Thom Parker (author) said:

    Hi Bill,
    pdfscripting.com does have some free content. But no, it’s not all free. We put an enormous amount of money and effort into putting this site together. There is no other resource available, book, website, or CD that offers this kind of organized, compresensive information on scripting Acrobat and PDF. And we are adding to it all the time. And something that we don’t advertise is that we offer limited consulting to members on topics that we have yet to cover. After all, you’d pay for a book. We think it’s quite resonable to ask for the small amount that we charge for acquiring much, much more than you’d get from any other resouce.

    You have to understand that anyone who provides useful information, whether it be on a forum, web site, giving a seminar at a conference, or any other resource, spends thier time and most likely thier own money doing it. You shouldn’t turn your nose up at such efforts. These are the people who are making things, they are creating and driving the industry.

    So the question is, what are you doing?

  • Thom Parker (author) said:

    Vijay,
    You’re welcome. The Acrobat JavaScript API is not always easy to use, and its certainly not obvious. Copying scripts seems like it should be easy, but as anyone whose done it knows, there are always pitfalls. I hope these tips will be a help to you in the future.

Leave your response!

You must be logged in to post a comment.