People ask me all the time what's the best way to learn Acrobat JavaScript. With any
other programming language, I would say that a beginner can learn quickest by studying
other people's code whenever the opportunity arises. But with JavaScript, I frankly
cannot (in good conscience) recommend this, because most of the JavaScript one
encounters in the real world is, in my experience at least, scandalously bad, a
frightful catalog of things-not-to-do.
The best way to learn Acrobat JavaScript is to spend 20 or 30 minutes a day
experimenting in the console. Hit Control-J or Command-J, type something in the
console, then select what you typed and click Enter on the numeric keypad. This will
execute your code in real time and show the evaluation result in the console. You can
execute any AcroJS method this way, and Acrobat will respond to your code instantly.
For example, if you execute app.addMenuItem() in the console (supplying
appropriate argument values, of course), you can add a menu command to any menu, on the
spot. You can also generally get help for an API method by supplying "?" (with quotes)
as an argument and executing the method in the console.
The quiz below covers some interesting "console one-liners" that have taught me quite a
bit about Acrobat JavaScript over the last six months or so. See how many of these
items you can figure out just by looking at them. Allow 15 minutes to work through the
20 questions. Score yourself according to the answers given at the end. (Note that many
of the questions involve methods and properties that are available only in Acrobat 5.0
or up. Also note that each code statement is executable exactly as
shown.)?
Explain what each statement does and what will appear in the console if you
execute the code there:
This (undocumented) call returns a string containing the device-independent
path to the Acrobat folder on your hard drive.?
Preincrementing the Doc property pageNum causes the current page view to
switch to the next page. Note that the parent designator 'this' is optional. The
default parent object for a property (if you don't specify one explicitly) is always
'this', so leaving 'this' off of Doc-object methods and properties is totally
acceptable as long as you have a document open already.
'openDoc' in app evaluates to true. It tests the existence of a
property (openDoc) in a given object (app). Since there is a method on the App object
called openDoc(), the statement is true as written. (Methods are simply properties that
point to functions.) The "this in that" syntax works because java-scripts "for/in"
looping construct uses it on a per-iteration basis.
app.activeDocs.lengthmore information."
this.getAnnots()[0].opacity = 0.6< sets the opacity of the document's first
annotation to 60%. It's undocumented and (of course) applies to Acrobat 5.0+ only.
path.split("/").pop()< returns the name of the current file (such as "AcroJS.pdf").
The String method split() simply breaks path into an array, using the
forward slash as a delimiter. The Array method pop() returns the last member
of the array.
app.addMenuItem({cParent:"Tools",cName:"-",cExec:""})< adds a separator bar to the
bottom of the Tools menu.
for(k in app)console.println(k)< dumps all App object property names to the
console.
external
returns a Boolean value indicating whether whether the current document is being
viewed in the Acrobat application or in an external window (such as a Web browser).
path
returns a string containing the fully qualified, device-independent path to the
current document.
spell.checkWord("shrek")< spellchecks the given word (in this case "shrek")
against the default dictionary and returns null if the word is spelled correctly. If
the word is not in the dictionary, you'll get back an array of suggested replacements.
In this example, we get back: shriek, shrike, shred, shark, shirk, shrug, shrewd,
creek, creak, shroud, shared, shored, shard, shirt, short.
Matrix2DAnnots.js (look in your
Acrobat/Javascripts folder). It's a general-purpose routine for transforming coordinate
systems. You will see the full source code if you simply type "Matrix2D" in the console
and hit Enter.
producer
tells you what kind of application generated the current document: for example,
"Acrobat Distiller 5.0 (Windows)".
color.toSource()< will return (as one big string) the source code for the "color
object," which is actually defined in AForm.js (look in your
Acrobat/Javascripts folder).
getField('t1').textFont = 'Verdana'< will set the font for your "t1" text field to
Verdana, assuming your document has a form field named "t1" and the current system has
Verdana in its font collection. The good news is that with Acrobat 5.0, you can finally
use any available font in your text fields. The bad news is that you have no way of
querying the host system to find out which fonts are actually available, and if your
user doesn't have the specified font, he'll see a disturbing message when he tries to
type something in the text field, along the lines of:
This is an ugly situation that Adobe needs to address pronto. Ideally, a font
enumeration (in an array property on the App object) should contain the names of
available fonts so that runtime discovery of fonts is possible. Failing that, the Field
object should have a setTextFont() method that returns a meaningful error
value.
app.focusRect=false< will turn off that ugly faint dotted line inside a button when
the button has focus.?
app.newDoc()<, with no argument values, will create a new, blank, single-page
8.5x11 document and bring it to the front.
bookmarkRoot.children[0].color=color.red< instantly changes the first bookmark's
color to red.
global.toSource()< will hand you a string containing the names and values of all
currently defined global variables, including functions (if any).
addAnnot().type="Circle"< adds a new Circle annotation, of diameter 100 pixels, to
the lower left corner of the first page of your document.
This quiz covers an enormous amount of ground, and the answers (admittedly short)
deserve more elaboration than space here permits. For now, let's just say that a little
experimentation in the JavaScript console yields enormous enlightenment.
If you got more than half of these problems right without looking in AcroJS.pdf or
"cheating a little" in the console, you can consider yourself a dangerously
knowledgeable practitioner of the scripting arts. May your children inherit your
methods.
If your acquaintance with Acrobat 5.0 scripting methods is merely casual, getting more
than five questions right means you're well on your way to becoming a deviously
resourceful Acrobat JavaScript programmer. (Even if you got no questions right, you
shouldn't feel bad. This was an extraordinarily challenging quiz.)
Whatever your score, I hope you learned one or two things and discovered a ripe area or
two (or five, or ten) for further exploration.
Despite the numerous benefits, there can be potential issues with the conversion of paper documents into electronic archives. When scanning paper pages into PDF, it's possible to end up with the odd- and even-numbered pages in separate PDF files. It can be very time-consuming to collate them manually, but there is an easier way. Sean Stewart explains.
BCL easyPDF SDK is a set of PDF Programming Libraries designed specifically to help Software Developers / Programmers build and deploy enterprise class PDF applications for corporate wide PDF...