Download Full Beginning JavaScript for Adobe Acrobat A non programmer s Guide John Deubert PDF All Chapters
Download Full Beginning JavaScript for Adobe Acrobat A non programmer s Guide John Deubert PDF All Chapters
com
https://ebookname.com/product/beginning-javascript-for-
adobe-acrobat-a-non-programmer-s-guide-john-deubert/
OR CLICK BUTTON
DOWNLOAD EBOOK
https://ebookname.com/product/adobe-acrobat-8-for-windows-and-
macintosh-visual-quickstart-guide-1st-edition-john-deubert/
ebookname.com
https://ebookname.com/product/ivor-horton-s-beginning-
visual-c-2010-wrox-programmer-to-programmer-1st-edition-ivor-horton/
ebookname.com
https://ebookname.com/product/learning-javascript-design-patterns-a-
javascript-and-react-developer-s-guide-2nd-edition-adnan-osmani/
ebookname.com
https://ebookname.com/product/saints-and-sinners-a-history-of-the-
popes-fourth-edition-eamon-duffy/
ebookname.com
Northwest Coast Indian Art An Analysis of Form 50th
Anniversary Edition Bill Holm
https://ebookname.com/product/northwest-coast-indian-art-an-analysis-
of-form-50th-anniversary-edition-bill-holm/
ebookname.com
https://ebookname.com/product/handbook-of-energy-audits-sixth-edition-
albert-thumann/
ebookname.com
https://ebookname.com/product/the-george-gershwin-reader-readers-on-
american-musicians-1st-edition-edition-robert-wyatt/
ebookname.com
https://ebookname.com/product/worlds-of-music-an-introduction-to-the-
music-of-the-world-s-peoples-fifth-edition-jeff-todd-titon/
ebookname.com
https://ebookname.com/product/the-sociology-of-gender-and-the-family-
middle-class-families-1st-edition-colin-bell/
ebookname.com
Cardiovascular Disease in the Elderly 4th Edition Wilbert
S. Aronow
https://ebookname.com/product/cardiovascular-disease-in-the-
elderly-4th-edition-wilbert-s-aronow/
ebookname.com
switch (itemPicked) {
{
Statement.nextRow() case "See close-up":
ar row = gStatement.getRow() this.pageNum = 2
opulateFields(row) var gHelpText = [
Beginning
ostMessage("") “What’s your full name?”,
“Where do you live?”,
break
case "About the artis
Adobe Acrobat®
app.beep()
app.alert("Your order has been submitted.")
A non-programmer’
}
s guide
else {
if (pwd != null)
app.alert("That is not the password!\n\n
¬ (Hint: it’s a small, rather repulsive amphibian.)")
}
E
var re10Digits = /^(\d{3})(\d{3})(\d{4})$/
John Deubert
if (re7Digits.test(event.value))
QDGuides
var orderInfoTemplate = this.getTempla
var addressTemplate = this.getTemplate
Quality Electronic Documentation
event.value = RegExp.$1 + "-" + RegExp.$2 from Acumen Training
ISBN-13: 978-0-9850512-0-4
ISBN-10: 0-985-05120-5
Version 1.1
Notice of Rights
All rights reserved. This book may not be redistributed to another computer.
Notice of Liability
The information in this book is distributed “as is,” without warranty. While every precaution has been taken
in the preparation of the book, the author shall not have any liability to any person or entity with respect to
any loss or damage caused or alleged to be caused directly or indireclty by the instructions contained in this
book or by the computer software and hardwre products described in it.
Trademarks
Trademarks are used throughout this book. Rather than put a trademark symbol in every occurrence of a
trademarked name, we aver that we are using the names in an editorial fashion only and to the benefit of the
trademark owner with no intention of infringement of the trademark.
–i–
QED Beginning JavaScript for Adobe Acrobat
Table of Contents
2 Page and Document JavaScripts 16 12 Formatting Text Fields with Regular Expressions 94
In which we learn the basics of creating and editing JavaScripts in Acrobat. In which we use regular expressions to automatically re-format user’s text input.
–ii–
QED Chapter 0
Introduction
You are about to open a new chapter in your work with Acrobat forms. In reading this book, you will learn
how to add features and abilities to your forms that are not otherwise possible: roll-over help, automatic text
field formatting, database connectivity, and more will all become routine parts of your form design. You will
also learn a programming language, perhaps your first: JavaScript. Even if you’ve never had a hankering to
write software and sling code, you’ll find JavaScript an important arrow in your quiver; it’s supremely useful,
relatively easy, and surprisingly fun.
Of course, you don’t need JavaScript skill to create an Acrobat form; you’ve no doubt been doing perfectly
well for quite some time without it. However, a knowledge of JavaScript adds immeasurably to your ability
to make your forms look and behave exactly as you want. JavaScript will open to you a world of possibility
whose scope is hard to overstate. Sufficient to say that the knowledge and techniques you will learn in this
book will allow your forms to take on a sophistication that is otherwise completely impossible.
This Book
If you are an experienced Acrobat form designer, but have never written a line of programming code in your
life (and pretty much wanted to keep it that way), then this book is written for you. Together we’ll explore
programming concepts while learning how to add features to your Acrobat forms: we discuss arrays while
creating a dynamic list of prices; we talk about case statements while creating a pop-up menu; we teach a
form to check the version of Acrobat on which it’s running and, along the way, talk about if–else constructs.
When you’re done with the book, you will be in good shape to read more formal books on JavaScript and to
make use of Adobe’s technical specification of Acrobat JavaScript.
–iii–
QED About the Book
What this book isn’t
This book is not a complete reference to the JavaScript language or to using JavaScript in Acrobat. JavaScript
is a broad and deep language and is capable of much that we don’t discuss here.
The purpose of this book is to bootstrap you to a point where you can learn the rest of the language’s
abilities on your own; it assumes you are an experienced Acrobat forms designer, but have little or no
experience with computer programming languages. If you are already comfortable with Objective C, Java, or
other programming language, this book will be paced too slowly for you; you should go directly to Adobe’s
Acrobat JavaScript Object Specification.
Also, this book does not teach you how to make form fields and other components of Acrobat forms;
I assume you have reasonable experience with the mechanics of designing and creating forms.
Mac or Windows?
With one exception the examples in this book will work with either the Macintosh or Windows version of
Adobe Acrobat. The exception is the pair of chapters on Acrobat database connectivity, which is available
only in Windows. Otherwise, the Mac and Windows versions of Acrobat work identically, except for minor
differences dialog box labelling, etc. Illustrations in this book are taken from both versions of Acrobat.
Some chapters need to be read in order; for example, the discussion of regular expressions is spread across
Chapters 10, 11, and 12 and they must be read in that order to be sensible at all. Most chapters, however, are
intended to be semi-independent.
All of the chapters, however, assume you have read Chapters 1 and 2.
–iv–
QED About the Book
Reading the book on-screen
Being an e-book, Beginning JavaScript for Adobe Acrobat is distributed electronically; its pages are designed
to be easily read on-screen. Since it’s distributed as a self-contained pdf file, you can read the book on nearly
any computer or tablet. It turns out that the page size works particularly well on a 768x1024 screen, such as
that of the iPad; that’s probably a coincidence.
The pdf file has been “enabled” for commenting in Adobe Reader, so you should be able to make notes to
yourself on the pages in either the full Acrobat or Reader.
URL links within the book are all “live” and will open the appropriate page in your default web browser.
If you register your book purchase (go to www.acumentraining.com/QEDGuides/qedregister.html), you’ll Figure 0.1 This book’s page size works well printed two-up on A4 or
American Letter-size paper.
be notified whenever there is a new update for your book. It’s worth doing.
–v–
QED About the Book
Online Resources
There are two sources of information that are specifically intended to be used with this book.
■ The complete, functioning form, with all JavaScripts in place. This shows you how the form is supposed to
behave and allows you to inspect the chapter’s JavaScripts in place. This version of the form has a filename
ending with “end.”
■ A version of the form with all form fields and other elements in place, but no JavaScripts attached. Use
this version if you want to follow along while reading the chapter, writing, debugging, and executing the
JavaScripts as you go. The file for this version of the form will have a name ending in “start.”
The sample form files are available for downloading at www.acumentraining.com/QEDGuides/acrojs.html. This
web page is also where you will find errata, update information, and other useful information.
Figure 0.2 Each issue of the Acumen Journal has an Acrobat-related article; many of
these are about JavaScript usage.
Acumen Journal
The Acumen Journal (Figure 0.2) is a free periodical that I produce three or four times per year. Each issue has
an article on advanced Acrobat usage and many of these are about JavaScript. The back issues make a good
supplemental to the topics in this book; there are more than 60 issues accumulated since I began writing the
Journal back in 2000. You can download them at www.acumentraining.com/acumenjournal.html.
If you wish to be notified when a new issue of the Journal comes out, there is a link on the Acumen Journal
web page to an appropriate form.
Thank You
Finally, thank you for buying this book or looking at the sample chapters. Either way, you are participating in
an ongoing experiment in publishing.
–vi–
QED Chapter 1
Welcome to JavaScript
Periodically in life, you learn something that broadens your world immeasurably, revealing an expanse of
What We’ll Learn in this Chapter
new experience, problems, opportunities, play, and work. Whole worlds that had invisibly surrounded
In this chapter, we’ll learn:
you suddenly appear, providing a new space to explore. Reading, sex, driving, children, all bring with them
concerns, interests, and interactions that had been previously inaccessible and unsuspected. ■ What’s a JavaScript?
In its own small way, learning JavaScript will be just such a threshold event in your professional life. If you’ve ■ Types of JavaScript
been working with Acrobat for any length of time, you’ve probably gotten pretty good at it and have ■ Attaching a JavaScript to a form field
become quite comfortable at creating forms, adding music, creating slide shows, and all the other features
■ JavaScript objects, data types, and syntax
Acrobat offers.
■ JavaScript errors and the debugger
This book introduces the New World. A knowledge of JavaScript allows you to do things within Acrobat that
■ How to use your own text editor with Acrobat
far exceed what you’ve done so far: You can interface with databases; add your own pop-up menus; create
JavaScripts
forms with sophisticated, interactive interfaces; and implement form fields that can look up p rices and other
data. These are only a few of the things you can do within your Acrobat documents using JavaScript. The
extent to which you can manipulate your Acrobat files is vastly greater with JavaScript skills than without.
Here you will learn how to accomplish a variety of useful tasks in Acrobat using JavaScript. Along the way, you
will learn a great deal about JavaScript, programming, and Adobe Acrobat.
Beyond that, this book does not assume any knowledge of programming; you will learn the programming
skills you need as we proceed through our examples.
Again, this book is intended for programming novices; if you are an experienced programmer, you may it to
be paced slower than you’d like. Experienced programmers may do better to just go to a more-advanced
book to learn JavaScript (there are myriad such books, though they all teach JavaScript in the context of web
pages) and then look at Adobe’s JavaScript reference site to see how to apply it to Acrobat.
–1–
QED 1. Welcome to JavaScript
What Version of Acrobat Should You Have? Books on Acrobat Forms
Although this e-book presumes you are working with Acrobat XI, the current version, it works equally well Although there are a number of books that will teach you how
with Acrobat X, whose interface is nearly identical. The instructions we step through will nearly all apply to to create Acrobat forms., most of them are out of print. The only
Acrobat 9, as well. Where there is a large difference in Acrobat 9, I’ll provide a note on what you should do extant book is:
in the earlier software. • PDF Forms Using Acrobat and LiveCycle Designer Bible
Ted Padova and Angie Okamoto
What Is JavaScript? Ted Padova is the king of the “Bible” style books and this
volume shows why.
JavaScript is a programming language. The term “programming language” often induces jitters in n ewcomers,
but, conceptually, it’s not very scary: A programming language is a language that is used to describe the There are other books specific to Acrobat forms that, though
steps involved in carrying out some task. In Acrobat, these tasks include moving to a particular page of a out of print, are still available if you look around. In particular, my
own old book is still available, though it’s getting pretty long in
document, sending data to a database, and calculating a form field value. Carrying out the steps described
the tooth, I admit:
by a JavaScript is referred to as executing the program.
• Creating Forms in Adobe Acrobat – John Deubert
As a programming language, JavaScript’s most significant characteristic is that it’s simple enough that many
Finally, any reasonably complete book on Adobe Acrobat will
applications use it as their built-in scripting language. Web browsers can all interpret JavaScripts embedded
have at least a chapter or two on creating forms. This includes my
in Web pages, and, particular to our topic, Acrobat can execute JavaScripts attached to form fields, pages,
own book:
and pdf files.
• Adobe Acrobat X: Visual Quickstart Guide – John Deubert
Like any language, JavaScript has its own vocabulary (words that have meaning) and syntax (rules by which
• Acrobat X Classroom in a Book – Adobe Creative Team
you make statements with those words). Learning JavaScript, therefore, has much in common with learning a
human language, such as Spanish or German, only it’s much easier. JavaScript is vastly simpler than any human • Acrobat X PDF Bible – Ted Padova
language: there are no metaphors, no literally nonsensical idioms, no synonyms, no subtle shades of meaning. These are all available through Amazon.com, as is pretty much
Just very precise statements telling Acrobat to do something very specific. everything in the world.
JavaScript in Acrobat
Acrobat allows you to create five different kinds of JavaScripts:
■ Form Field JavaScripts are attached to form fields. Acrobat executes the script when a particular event
occurs in that form field, such as a button click. Most JavaScripts in Acrobat are attached to form fields.
■ Page JavaScripts are executed when the user moves to or leaves a particular page in the Acrobat document.
■ Document Action JavaScripts are executed user opens, closes, saves, or prints a document.
–2–
QED 1. Welcome to JavaScript
We shall talk about Page, Document, and Document Action JavaScripts in Chapter 2; we won’t be discussing
application JavaScripts at all in this book. For now, let’s look at how you type in and use a Form Field JavaScript.
This form consists of a set of flash cards that are intended to be printed double-sided and then used to quiz
students on vocabulary terms. Our pdf file has only a few flash cards; each card has a button that takes u sers
to an order form they can use to purchase the complete set of cards. We are going to add a JavaScript to
the Order Form button that takes the user to the order form, located on the last page of the Acrobat file
(Figure 1.2).
As will be true throughout this book, there are two versions of this form on the JavaScript for Acrobat web page:
–3–
QED 1. Welcome to JavaScript
Attaching a JavaScript to a Form Field
As a reminder of something you may already know, let’s step through the process of attaching a JavaScript to
a button, in this case our Order Form button.
Figure 1.3 Enter Form Editor mode by
To attach a JavaScript to a button: clicking the Edit button in the Forms
x Note
panel.
Start with the form open in Adobe Acrobat and the Tools
pane exposed. Acrobat 9 doesn’t have a Tools pane, but
it does have a Forms menu. To get to the Form
1 In the Tools pane’s Forms panel, click on the Edit tool Editor, select Forms > Add or Edit Fields. Having
(Figure 1.3). done this, you can follow the steps exactly as listed.
Acrobat will present you with the Field Properties dialog box (Figure 1.5). Figure 1.4 Acrobat’s Form Editor mode gives you a Forms pane, new
toolbars, and presents each form field as a rectangle with handles..
3 Click on the Actions tab.
You will now be looking at the set of controls that specify what
should happen when you click on this button (Figure 1.6).
This tells Acrobat that this button’s action should trigger when
the mouse button is released after clicking in the b utton. (See
the sidebar, at right.)
–4–
QED 1. Welcome to JavaScript
6 Type your JavaScript into the text field of this dialog box. Form Field Events
In the case of our order form, the JavaScript is a two-line The Select Trigger pop-up menu, shown in Figure
program that moves the user’s view of the document to 1.6, offers six form field events to which you can
the page containing the order form and then causes the attach an action:
user’s computer to beep: ■ Mouse Down occurs when the user presses the
mouse button with the pointer in the form field.
this.pageNum = 6
app.beep() ■ Mouse Up occurs when the user clicks on the
field and then releases the mouse button with
Type these lines into the text-editing field exactly as the mouse pointer still in the field.
above, making sure to match upper and lowercase. The Figure 1.7 The JavaScript Editor window is a simple text editor that you
first line of code says, “In this document, set the current ■ Mouse Enter occurs when the mouse pointer
will use to type in your JavaScripts.
first rolls over the form field.
page number to 6.” The second line tells the Acrobat
application to beep. ■ Mouse Exit occurs when the mouse pointer rolls
out of the form field.
7 Click the ok button of the JavaScript Editor and the Close
■ On Focus occurs when the user clicks on or tabs
button of the Field Properties dialog box to return to the
into the form field, so that it becomes the target
Acrobat form.
for keyboard or other input.
You are now looking at the Acrobat flash card page with ■ On Blur occurs when the user tabs out of a form
the Form tool still selected, as in Figure 1.4. field or clicks on some other form field, so that
our field is no longer the target for user input.
8 Exit the Form Editor by clicking the Close Form Editing (Blur is the opposite of Focus, of course.)
button in the Forms pane (Figure 1.8).
The Field Properties dialog box, Figure 1.6, lets you
You are now back where you started, looking at your associate one or more A ctions (a JavaScript action,
Figure 1.8 You exit the Form Editor by clicking Close Form Editing button.
document page. in our case) with any of these events.
This button is in the Forms pane in Acrobat X (above right) and above the
pane in the newer Acrobat XI (above left).
So, now try it out. Click the Order Form button, and Acrobat
will move to the order form page and then beep.
JavaScript Objects
Our two-line JavaScript makes use of two JavaScript objects. A JavaScript object is the representation of some
piece of data within your JavaScript program. Before your program can manipulate or examine a form field, it
must first create an object that represents that field. Most of the things you can manipulate in JavaScript (pages,
signatures, database connections, and so on) are represented in your program as objects.
In our sample program, this refers to a Doc object. A Doc object represents an open Acrobat file to your
JavaScript program; you use this object to change pages, save the document, and otherwise manipulate the
–5–
QED 1. Welcome to JavaScript
document from within your program. The word this in our sample JavaScript refers particularly to the A crobat
document in which our JavaScript resides (the Flash Cards file, in our case); think of it as short for this document.
The word app is an App object, a reference to the Acrobat application being used to view the current
document. You use an App object to tell the Acrobat application to do something: open a file, put up an
alert dialog box, or, in our case, beep.
■ Annot represents an annotation (for example, a “sticky note”) in the current document.
■ App represents the Acrobat application being used to view the current document.
–6–
QED 1. Welcome to JavaScript
The phrase this.pageNum addresses the pageNum property of the Document object; this property is the “Program” vs. “Script” vs. “Code”
page number the document is currently displaying to the user. Our program moves the user to the order
Here are three closely related terms that we’ll be using
form page by setting the current document’s pageNum property to the order form’s page number:
throughout this book.
this.pageNum = 6
■ A program is a general term for a series of instructions that
Some observations about this page number assignment: tell a computer in detail how to carry out a particular task. In
general, a program is a stand-alone set of instructions, such as
■ You address the property of an object by naming both the object and the property, joined by a period: an application.
Object methods
A method is a command that is associated with a JavaScript object. Just as a dog can be given commands
(“Sit,” “Heel,” “Spit that out this instant!”), JavaScript objects have commands that they can carry out. The set of
commands is different for each type of object. For example, Table 1.2 lists some of the commands the app
object knows how to execute.
–7–
QED 1. Welcome to JavaScript
In our Order Form JavaScript, we executed (“called”) the app object’s beep method:
app.beep()
Note that we call an object’s method in the same way that we refer to one of its properties: the object
name, a “dot,” and the method name. The method name is followed by parentheses enclosing the method’s
arguments; let’s talk about what those are.
The above JavaScript statement would open an Acrobat file named TermPaper.pdf. Note that our earlier call
to app.beep had empty parentheses because the method doesn’t take any arguments; you still need the
parentheses, though.
Sometimes when you give a dog a command, you expect the dog to give you something back: the command,
“Fetch the stick, boy!” should yield a stick in your hand (along with some gratuitous drool). S imilarly, many
JavaScript methods have a return value, some piece of data they give back to the JavaScript program.
The openDoc method we invoked above actually returns a Doc object representing the newly opened
document, though our single-line use of openDoc just ignores it. We shall look at return values in much more
detail in the next chapter.
Named Arguments
Generally, you must supply arguments to a method in a certain order; the app object’s alert method, which
we’ll talk about in detail in the next chapter, wants a string and an icon code:
app.alert("Woah! Somethin' weird just happened!",2)
The string and the integer must be supplied in that order so that JavaScript can identify them.
However, you can also pass arguments to a method by name. The Acrobat JavaScript Guide (described at the
end of this chapter) defines a name associated with each of the arguments a method requires; in the case of
app.alert, the names are cMsg and nIcon.
–8–
QED 1. Welcome to JavaScript
You can supply these arguments, in any order, using the following call to the method:
app.alert({cIcon: 2, cMsg:"Woah! Somethin' weird just happened" })
Note that we have braces within the method’s parentheses and within those we have our method arguments.
Each argument is represented by the name of the argument, a colon, and then the argument’s value; the
arguments are separated by commas.
This is not as concise as passing the arguments by position, but it is clearer as to the purpose of each of the
arguments. (Our first call to app.alert, for example, gives no clue to the purpose of the 2.) Also, passing
arguments by name give you great flexibility in formatting your code. In particular, you can place line breaks
within the argument list and supply the arguments in any order; our previous example could have been written
app.alert({ cIcon: 2,
cMsg: "Woah! Somethin' weird just happened" })
In this book, we shall pass arguments by position; we’ll use argument names only when it’s necessary—usually
for clarity—to a particular JavaScript example. I shall, however, often supply the names of the arguments when
describing methods so that you can use them if you wish.
JavaScript Statements
A JavaScript program—any computer program—consists of a series of statements, each of which carries out
one step in the overall task. Our sample program consists of two statements: a page assignment and a call to
the app object’s beep method.
this.pageNum = 6
app.beep
Usually, each line within a JavaScript program will contain a single JavaScript statement, as in our program. You
can put more than one statement on a line, separated by semicolons. Our two-line program could have been
written on a single line:
this.pageNum = 6; app.beep
–9–
QED 1. Welcome to JavaScript
Why would you do this? Purely for esthetics; some people just prefer to combine very simple statements
together. I recommend against this practice; most programs are much easier to read if you have only one
statement per line.
If you read other people’s JavaScripts, you may notice that many programmers put semicolons at the end of
every line in their program:
this.pageNum = 6;
app.beep;
This doesn’t hurt anything, but it’s unnecessary. Most of them do it out of habit; JavaScript looks very much
like the programming languages C and C++, both of which require that all statements end with s emicolons.
You can leave out the semicolons.
JavaScript Text
JavaScript programs are simply text files; you can write them with any text editor or word processor and then
copy and paste them into the Acrobat JavaScript Editor dialog box. In fact, Acrobat lets you specify an e xternal
editor that should be used for editing your JavaScripts; we’ll discuss how to do this at the end of the chapter.
By the way, space and tab characters within a JavaScript line have no particular meaning in JavaScript. You can
use them as you wish to format your program. This is a purely visual issue; you want to format your JavaScript
code so that it’s easy to read.
Use whitespace characters lavishly! Reading program code is tedious at best; a program can be nearly
undecipherable if the programmer has not formatted the code for easy reading. This is an important enough
issue that I shall be providing formatting tips for many of the JavaScript constructs we use in this book.
JavaScript Comments
JavaScript code can be pretty cryptic. Puzzling over someone else’s code (or even your own code from six
months ago), trying to figure out exactly what it’s trying to do, can be tedious. As a courtesy to others looking
at your code and as an aid to your future self, it is very important to place comments in your JavaScript code.
A JavaScript comment is text in your code that is ignored by the JavaScript “machine.” The purpose is to let
you place your own notes to be read by human beings examining the code.
–10–
QED 1. Welcome to JavaScript
JavaScript recognizes (that is, ignores) two kinds of comments:
■ Single-line comments start with a double slash (//) and extend to the end of the current line in the
JavaScript code. These are intended for brief comments.
// This is a single-line comment.
■ Block comments start with a /* and end with a */. Between these two delimiters can be as much text as
you wish, spread out over as many lines as you wish within the JavaScript file. Use this for longer comments.
/* Here we have a block comment.
This text will be completely ignored
until we end the comment, right here. */
An Example
Consider the following, uncommented JavaScript from later in this book:
var txtField = event.target
txtField.fillColor = color.red
txtField.textColor = color.white
Since we have not yet discussed these commands, the exact purpose of this script and how it carries out that
purpose is very unclear, although it does seem to have something to do with color.
On the other hand, if we include comments in the code, then it becomes possible for someone unfamiliar
with the program to at least know what the intent of the program is and generally what it’s doing:
/* This program changes the background and text color of
a text field when the user tabs into or clicks in the
field. */
var txtField = event.target // Get a reference to the text field
txtField.fillColor = color.red // Set the background to red
txtField.textColor = color.white // Set the text color to white
This version of the program is much clearer, even to someone new to the code.
Comments are a Force for Good in programming. Any script more complex than a couple of lines should
include comments that describe what it does and how it works.
All of the examples in the rest of this book will be heavily commented to make them as comprehensible as possible.
You’re welcome.
–11–
QED 1. Welcome to JavaScript
JavaScript Errors
In the (ahem) rare event that you have an error in your JavaScript—you misspelled a variable name, mis-copied
a piece of code, and so on—you will be faced with the task of figuring out what is wrong with your code,
a process known as debugging. Acrobat left to itself treats these errors quietly; if the code fails, Acrobat
just aborts the script; to all appearances, clicking on the button did nothing at all, although there was really
a failed script h appening beneath the hood. This isn’t useful for debugging; if a JavaScript fails, we’d like to
know about it and, furthermore, be told what went wrong, so we can fix it.
To get this diagnostic information about failed scripts, we need to enable something called the JavaScript Debugger.
With the JavaScript Debugger enabled, whenever one of your JavaScripts fails, Acrobat will open the
Debugger with an error message (“ReferenceError,” in Figure 1.9). This will allow you to determine what went
wrong and what to do about it.
Error messages can be somewhat cryptic at first, but with time and familiarity they become useful. The most
common messages you are likely to see are the following:
This indicates that you misspelled something; the name “xxx” (or whatever) is not one that JavaScript Figure 1.9 The JavaScript Debugger window presents information about errors in
knows. Remember that JavaScript is case-sensitive; there is a difference between app (which JavaScript your JavaScript, as well as providing a variety of other tools.
knows) and App (which it doesn’t know).
■ Syntax Error
This means that JavaScript could not make sense of something in the code. This usually means you omitted
something (a comma, a number, a parenthesis) from your script. An example of a syntax error would be
app.alert("Hi, Mom", 3
The set of possible error messages is very large, though most are pretty hard to provoke. Just sit tight, read
the message, and carefully examine the aberrant JavaScript code for misspellings and omissions.
–12–
QED 1. Welcome to JavaScript
Enabling the JavaScript Debugger
You enable the JavaScript debugger in the Preferences of your copy of Acrobat. Open your Acrobat
preferences and select JavaScript in the long list of preference categories (Figure 1.10). All of the checkboxes
in the JavaScript Debugger section should be selected, as in the figure.
You can ask Acrobat to use text editing software of your choice to edit your JavaScripts. Having told A
crobat
Figure 1.10 You enable the JavaScript debugger in your Acrobat preferences. Select
what editor you want to use, it will automatically launch this software when you click on the Add or Edit
all of the checkboxes in the Debugger section, as above.
button in the Document JavaScripts dialog box (Figure 1.11).
To set this up, you must specify in Acrobat’s Preferences the editor you wish to use for working with JavaScripts.
Acrobat will present you with its Preferences dialog box (Figure 1.12, next page).
The Preferences dialog box will display the controls that affect Acrobat’s JavaScript support, as in Figure 1.12.
3 Among the JavaScript Editor controls, at the bottom of the dialog box, select Use External JavaScript Editor.
Figure 1.11 Acrobat will launch your external text editor whenever you click the Ad
4 Click the Choose (Windows) or Browse (Mac) button and then navigate to the .exe or .app file for the
or Edit button in the Document JavaScripts dialog box.
editor you want to use when editing JavaScripts.
That’s all there is to it. Now, when you edit a JavaScript, Acrobat will automatically launch your text e ditor.
Type your JavaScript code into the text editor’s window, save the text, and then close the editor. Your
JavaScript will be automatically entered into Acrobat.
–13–
QED 1. Welcome to JavaScript
For what it’s worth, my favorite programmers’ editor on the Mac is TextWrangler (www.barebones.com); it
is a first-rate editor and completely free, omigawd. Among Windows text editors, I’m rather fond of TextPad
(www.textpad.com); it’s relatively inexpensive shareware and well worth the money.
JavaScript References
This book is a non-programmer’s introduction to using JavaScript within Adobe Acrobat; it offers full
coverage of neither the JavaScript language nor a complete description of the ins and outs of the Figure 1.12 You can tell Acrobat to use an external editor when typing or modifying
implementation of JavaScript that resides in Adobe Acrobat. When you are done with this book, you will JavaScript code.
have two other references you can use to further your studies.
Figure 1.13 shows a part of a page from the Reference. As you can see, this is not intended to be fun reading;
it’s a reference, not a tutorial. Still, if you want to learn the complete set of features that are built into the
JavaScript language (as opposed to the Acrobat version of it), this is the place to go.
–14–
QED 1. Welcome to JavaScript
Like the Client-Side Reference, the JSAPI is a technical specification, not a document you would willingly read
from one end to the other. It gives a detailed description of every JavaScript object type available in Acrobat
x
and the properties and methods of each. Where the book you are
Note
reading presents a series of examples of how to carry out specific
The JSAPI is available as a Kindle book from
tasks in JavaScript, the JSAPI describes everything you can do in amazon.com.
Acrobat with JavaScript.
To give you a bit of the flavor of the JSAPI, Figure 1.15 shows a screenshot of the complete description of the
app object’s beep method. I shall be making occasional r eferences to the JSAPI throughout this book.
Figure 1.15 The description of the app object’s beep method is a good
example of the type of description provided for every object., method, and
property in the JavaScript for Acrobat API Reference.
–15–
QED Chapter 2
Page and Document JavaScripts
There are five broad types of JavaScripts in Acrobat, each differing in where it’s used in the Acrobat document.
What We’ll Learn in this Chapter
■ Form Field JavaScripts are attached to form fields. As we saw in the previous chapter, Form Field scripts are In this chapter, we’ll learn how to:
associated with events that occur with a form field: Mouse Down, On Focus, and so on. ■ Run a JavaScript when a document opens
■ Document JavaScripts are associated with the opening of the Acrobat file. Acrobat executes these when ■ Run a JavaScript when a document closes
the document is first opened. ■ Run a JavaScript when entering or leaving a page
■ Document Action JavaScripts are executed when one of a set of predefined events happens with the ■ Using JavaScript variables
Acrobat file: the file closes, is saved, and so on.
■ Page JavaScripts are associated with a particular page. You can provide scripts that Acrobat will execute
when the user enters that page, leaves that page, or both.
■ Application JavaScripts are scripts that are executed by Acrobat when the application first opens. These
can perform a variety of interesting tasks, such as add items to the Acrobats menus. We won’t be talking
about application scripts in this book, because their use is specialized and infrequent; they’ll be a topic for
an eventual book 2.
We discussed Form Field JavaScripts in Chapter 1. In this chapter, we’ll look at how to write the other three
types of scripts.
The Project
(Files: JSAcro_Ch02_Example_Start.pdf, JSAcro_Ch02_Example_Final.pdf )
Our sample file for this chapter is the pet store catalog pictured in Figure 2.1. In the course of our discussion,
we’ll add examples of Document, Document Action, and Page JavaScripts to this file.
–16–
QED 2. Page and Document JavaScripts
The JavaScript Tools Panel
Several of the JavaScript-related tools we’ll be using in this chapter reside in the JavaScript panel in the
Acrobat X Tools pane (Figure 2.2).
If you examine your own copy of Acrobat, you’ll probably find the JavaScript panel is missing; A crobat Figure 2.2 The Javascript
doesn’t show this panel by default. (This is reasonable; the vast majority of Acrobat users don’t have anything panel lives in the Tools
pane. It is initially hidden.
to do with JavaScript and would be appalled at the prospect.) Before we can proceed further in this book,
we need to make the panel visible, thusly: The tiny icon in the upper-
right corner, just above
To make the JavaScript panel visible: the panels displays a
x Note
drop-down menu when
1 Make the Tools pane visible by clicking its name in the
clicked (Figure 2.3).
Acrobat X toolbar. Acrobat 9 doesn’t have a Tools pane. All of
the JavaScript commands in this chapter reside in
2 Click the tiny icon at the top of the Tools pane and select the Advanced>Document Processing submenu.
JavaScript in the resulting pop-up menu (Figure 2.3).
The JavaScript panel will become visible in the Tools pane. It will be available to your copy of Acrobat X until
you deselect it again.
Document JavaScripts
Document JavaScripts are executed by Acrobat upon opening the document to which they're attached. This is
a convenient way to present the reader with an initial “splash screen” or to carry out some other start-up activity
when the user opens your pdf file. You may attach as many Document JavaScripts to an Acrobat file as you wish.
As an example, we’ll add a Document JavaScript to our catalog that displays a greeting when the reader
opens the document (Figure 2.4).
Figure 2.3 Make the JavaScript panel visible by selecting it from the
To attach a Document JavaScript to an Acrobat document: Tools pane’s pop-up menu.
1 In the JavaScript panel, click the Document JavaScripts tool (back in Figure 2.2). Figure 2.4 We’ll start
by adding a Document
Acrobat will display the Document JavaScript dialog box (Figure 2.5 , next page). JavaScript that displays an
admittedly unattractive
2 Type a name for your script into the Script Name field. “welcome” message when
the pdf document opens.
–17–
QED 2. Page and Document JavaScripts
This can be any name you wish, although descriptive is
better than cryptic; we’ll name our script “Welcome,”
since that’s what it does.
This JavaScript displays the alert shown back in Figure 2.4. The app object’s alert method displays an
alert window with the specified text. The 3 indicates the kind of icon that should appear in the alert. We’ll
look at this method in more detail in Chapter 4. (You may recall we discussed the app object in C hapter 1.) Figure 2.7 Type your JavaScript into the text field of the JavaScript Editor.
Remember that JavaScript is case sensitive; your upper- and lower-case letters should match the line above.
5 Click ok to back out of the JavaScript Editor and return to the Document JavaScripts dialog box.
The dialog box will now show our Window script in its list of scripts (Figure 2.8). If you click on the
script in the list, the dialog box displays the code in the lower text field, as in the figure.
6 Click the Close button to exit the Document Scripts dialog box; you will now be back at your Acrobat
page. That’s it! We’ve created our Document JavaScript. Figure 2.8 Our
Welcome script is now
7 Try it out by saving the Acrobat file, closing it, and then reopening it. Acrobat should display the alert we listed in the D
ocument
saw in Figure 2.4. JavaScripts dialog
box. Click on the script
name and its text will
appear in the lower
text field.
–18–
Random documents with unrelated
content Scribd suggests to you:
Reverend and very dear Sir,
T HOUGH at this distance of time and place, yet I do not forget our
solemn and mournful parting. Blessed be God, there is a time
coming, when these cutting trials will be over. I long, I long for it,
and yet when fresh doors of usefulness are opening, I am content to
stay below. This I trust is the case at present in Virginia. The
prospect is promising indeed. People have flocked from all parts to
hear the word, and arrows of conviction have fled, and I believe
stuck fast. Seed sown several years ago, hath sprung up and
brought forth fruit. Doth not the Lord of the harvest by this say, “Go
forward.” Do, my dear Sir, help me by your prayers; I want to have
my tardy pace quickened. I am now forty years old. Surely it is high
time to awake out of sleep. I doubt not but this will find you and
yours all alive for Jesus. Being straitened for time, I must beg you to
remember me in the most tender manner to all dear, very dear
friends as they come in your way, and accept the same for your
whole self, from, reverend and very dear Sir,
G. W.
LETTER MLXXIII.
To Mr. F――.
I HOPE that this will find you safely arrived at Philadelphia, from
your northward expedition, and ready to come further
southwards. This leaves me in my last Virginia stage, near the
borders of North-Carolina. People as I came along have been very
willing to attend on my feeble labours, and I hope that some real
good hath been done. If this be effected, it matters little how the
labourer himself may be sometimes fatigued.
G. W.
LETTER MLXXIV.
To Mr. ――.
G. W.
LETTER MLXXV.
To Mrs. C――.
G. W.
LETTER MLXXVI.
To the Same.
G. W.
LETTER MLXXVII.
To the Reverend Mr. G――.
T HE love which I owe and bear to you for the Lord Jesus Christ’s
sake, constrains me to send you a few lines immediately upon
my arrival at my native country.—I arrived at New-haven in Sussex
the 8th instant, after a six weeks passage. Hither the cloud seemed
to move, though I must own that I left America with regret.—Never
was the door opened wider in those parts for preaching the
everlasting gospel, than now.—Perhaps this may shorten my stay at
home, but future things belong to Jesus. It is not in man to direct his
steps.—Prepare us, O Lord, for whatever thou hast prepared for us! I
hear, you have met with changes since my departure.—What have
we to do to expect any thing else? Dear Mr. M――n!—Whither is he
gone? Where Jesus reigns, and where, through rich and sovereign
grace, even ill and hell deserving I, hope ere long to be.—Hasten,
glorious Emmanuel, that wished-for time!—If your dear yoke-fellow
is also gone, she I trust is happy too.—Sequar, etsi non passibus
æquis.—You will write soon, and send me some news about the
upper world.—This is scarce worth a thought.—I hope my dear host
and hostess, Mr. S――, Mrs. M――, and all friends are well.—I can
only send them and you my hearty love, and beg the continuance of
your prayers for, very dear Sir,
Yours, &c.
G. W.
LETTER MLXXVIII.
To Lady H――n.
Ever-honoured Madam,
G. W.
LETTER MLXXIX.
To the Marquis of L――.
G. W.
LETTER MLXXX.
To Lady H――n.
Ever-honoured Madam,
I T hath given me great concern, that I have not been able till now,
to acknowledge your Ladyship’s most kind letter. I could almost
say, excess of business at my first coming over hath prevented me;
but to make the most of it, I do but little, and that little in so bad a
way, that if it was not for the compleat and all-sufficient
righteousness of the everlasting, ever-lovely Jesus, I could not lift up
my guilty head. And yet what a blessed week have we had! Surely,
sinners have come like a cloud, and fled like doves to the windows.
In about a fortnight, I hope to take the field in Gloucestershire, and
shall endeavour to make all possible dispatch in the west, in order to
wait on your Ladyship in my northern visit. Help me, thou friend of
sinners, to be nothing, and to say nothing, that thou mayest say and
do every thing, and be my all in all!—What a happiness is it to be
absorbed and swallowed up in God? To have no schemes, no views,
but to promote the common salvation. This be my happy lot! Your
Ladyship will still add to my innumerable obligations, by praying for
me. How shall I express my gratitude?—Tears trickle from my eyes,
whilst I am thinking of your Ladyship’s condescending to patronize
such a dead dog as I am. But it is because I belong to Jesus. He will,
he will reward your Ladyship openly. Ever-honoured Madam excuse
me. Tears flow too fast for me to write on. I must go and speak to
God for you and yours, as being, ever-honoured Madam,
Your Ladyship’s most dutiful, obliged, and ready servant for
Christ’s sake,
G. W.
LETTER MLXXXI.
To Governor B――.
Much-honoured Sir,
G. W.
LETTER MLXXXII.
To Mr. C――.
J UST now I was informed that the bag is to be taken away this
afternoon; hurried therefore as I am, you must have a few lines.
—And why? Because I love and honour you for Jesus Christ’s sake.
Will it not rejoice you to hear that his glorious gospel gets ground
apace. Several of the clergy, both in town and country, have been
lately stirred up to preach Christ crucified, in the demonstration of
the spirit and with power. This excites the enmity of the old serpent,
which discovers itself in various shapes. The greatest venom is spit
out against Mr. R――n, who having been reputed a great scholar, is
now looked upon and treated as a great fool; because made wise
himself, and earnestly desirous that others also should be made wise
to eternal salvation. Methinks I hear you say, O happy folly! May this
blessed leaven diffuse and spread itself through the whole nation!
The prospect is promising. Many students at Oxford are earnestly
learning Christ. Dear Mr. H――y hath learnt and preached him some
years; his loving and truly catholic heart chearfully complied with the
motion about your future correspondence. As for myself, I can only
say “that less than the least of all,” must be my motto still. I labour
but feebly, and yet, O amazing condescension! Jesus owns and
succeeds such feeble labours. People still continue to flock to the
gospel, like doves to the windows. Next week I begin to take my
country range. Be so good, my dear Sir, to add to my obligations, by
continuing to remember a poor but willing pilgrim, and to salute all
my dear friends as they come in your way. I hope, Mr. A―― (to
whom I send cordial respects) goes hand in hand with you, striving
together for the faith and the practice of the gospel.—Glorious strife
this! I do not forget the books I promised to look out for.—I have
spoken to Mr. H――, but he says they are very difficult to be
procured. I would write to the Chief Justice, but suppose he is at the
northward. Will you be pleased to accept of my Lisbon letters, to be
disposed of as you think proper? My little communion-book is not yet
come out. God be praised, that there is a time coming when we shall
need books and ordinances no more, but shall be admitted into an
uninterrupted communion and fellowship with the blessed Trinity for
evermore. The Lord ripen us for that blissful state! I must bid you
farewel. Be pleased to excuse the length of this, as being occasioned
by the love unfeigned which is due to you from, reverend and dear
Sir,
G. W.
LETTER MLXXXIII.
To Mr. P――.
London, June 12, 1755.
Dear Nat.,
LETTER MLXXXIV.
To Mrs. C――.
G. W.
LETTER MLXXXV.
To Mr. G――.
G. W.
LETTER MLXXXVI.
To the Marquis of L――.
My Lord,
G. W.
LETTER MLXXXVII.
To Mr. Thomas J――.
My dear Thomas,
Yours, &c.
G. W.
LETTER MLXXXVIII.
To Lady H――.
Ever-honoured Madam,
LETTER MLXXXIX.
To the Reverend Mr. H――n.
My dear Friend,
I RECEIVED your kind letter, and have since heard of your removal
from Indian land. I cannot help commending you for it, since I
know various places, where you might do as much good in a week,
as in those parts for years. Poor Indian land! I fear thou hast had
thy call! May Jesus secure the remaining few, and be their refuge
from every impending storm! O my dear friend, you and I shall find
more and more, that we must be made perfect by sufferings. If we
do not meet them in our younger days, we shall certainly have them
in the decline of life. Trials, at such a season, are like the finishing
strokes of the Limner’s pencil. They serve, in the hands of the holy
Spirit, to compleat the new creature, and make it fit for heaven.
Happy they who can say, “He knoweth the way that I take:” when
they are tried, they shall come forth like gold. God only knows what
a trying season lies before us. It is to be feared, that we are upon
the eve of a bloody war. O that the war between Michael and the
Dragon may go on! The prospect is promising. Several ministers
preach Christ boldly; and as for my own poor feeble labours, the
blessed Jesus vouchsafes to crown them with success. So many
paths lie open, that I can scarce tell which to take first. Through
frequent preaching and travelling, and a multiplicity of business, I
have been so weakened, that I am obliged to retire for a little
relaxation, and to get leisure to write you these few lines. May they
find you and yours rejoicing in tribulation, and increasing with all the
increase of God! I commend you, and all enquiring friends, to the
care of Him, who never faileth those who trust in him, and entreat
the continuance of your prayers in behalf of, my dear friend,
G. W.
LETTER MXC.
To Mrs. C――.
I WRITE this from Mr. C――’s, whither I have retired for a little
relaxation, and to get leisure to write a few letters. Your brother
is just gone from me, and would send you his hearty love if he knew
of my writing. T―― is gone to Plymouth, and Miss L―― is sick of a
fever (though I hope not dangerously) at Lewisham. Mrs. W――,
one of the conference, I buried last Tuesday; and am longing for
that happy time when this frail tabernacle shall be also laid in the
silent grave. It hath tottered lately more than ever, and yet the
Redeemer vouchsafes so to comfort me within, that methinks I am
not yet to die, but live and declare the works of the Lord. O that I
may begin to begin to do something for my God! I am afraid we are
at the eve of a war: but fear not. Bethesda especially, I believe, will
be hid under the shadow of the Redeemer’s wings. Whilst I have
persons there (as I think is the case now) that act with a single eye,
I am persuaded it will be like the burning bush. Pray remember me
to all in the kindest manner. I hope to write when the childrens
cloaths are ready. We have blessed seasons at Tabernacle. I have
met with some outward as well as inward trials; but this is my
comfort, “The Lord knoweth the way that I take. When I am tried I
shall come forth like gold.” O that faith and patience may have their
perfect work in this sin-sick soul! Desire dear Nat, Mr. Dix, &c. not to
be uneasy, because I do not write now; it is impracticable. Night and
day you are all upon my heart, and I long to hear how you go on.
My blessing attends you all, even you and the children. Surely, was I
retired from public work, feeding Christ’s lambs should be my
constant employ. But alas! I must be in the front of the battle. Lord
Jesus, magnify thy strength in my weakness, or I shall deny thee and
desert my post! Pray hard all of you, I intreat you, for me, and let
me know all your wants. I will labour night and day to have them
supplied, and will pray the great Shepherd and Bishop of souls to
pay you blessed wages. I know you will be content with such a
paymaster. I commend you all to his never-failing mercy, as being,
dear Mrs. C――,
Your most sincere and cordial friend and willing servant for
Christ’s sake,
G. W.
LETTER MXCI.
To Lady ――.
Elect Lady,
G. W.
LETTER MXCII.
To Mr. J―― W――.
G. W.
LETTER MXCIII.
To Doctor R――.
G. W.
LETTER MXCIV.
To ――.
My dear Friends,
I RECEIVED both your kind letters, and likewise one from Mr.
W――, and last night a long one from Mr. C――; but alas! I have
no time for controversy. To their own Master they must both either
stand or fall. All I can say in your present circumstances, is, “that
you had best make a trial, and let matters for a while stand as they
are.” I have sent letters, if possible, to prevent the spreading, at
least the publishing any further tales. In the mean while, my dear
friends, do you strengthen yourselves in the Lord your God. The
cause is his; you honestly, I believe, embarked in it for his great
name’s sake, and he will help you out of all. When I see Mr. R――r, I
shall speak to him again. But to-morrow I must away to the
northward. Follow me, I entreat you, with your prayers, and assure
yourselves, that you and yours, and the dear people of Norwich, will
not be forgotten by me. If ever the Redeemer, in his good
providence, should bring me thither again, I can then converse with
Mr. W―― and C―― face to face;—but I beg to be excused from
writing, when I think by so doing I can do no service. The Lord
cloathe us all with humility, and give us all true simplicity and godly
sincerity! Then we need fear nothing. Blessed be his name, we have
golden seasons here. Blessed be his name, if any good was done by
my poor feeble labours in your city. To Him, who delights to shew
himself strong in behalf of those who are of an upright heart, be all
the glory. If I know any thing of myself, I want to supplant none, but
to strengthen the hands of all that preach and live Christ Jesus. In his
great name, and with repeated thanks for all favours, I subscribe
myself, my very dear friends,
G. W.
LETTER MXCV.
To Miss P――.
Dear Miss,
I could enlarge, but must away. My heart is full for dear New-
England, and I must go to God and vent it. I can only send you and
your honoured parents, and all your religious associates, and all
enquiring friends, ten thousand thanks for all favours, and beg them
never to cease praying for, dear Miss,
Their and your assured and ready servant for Christ’s sake,
G. W.
LETTER MXCVI.
To Lady P――.
Dear Madam,
G. W.