VBJava Script
VBJava Script
Programming Acrobat
JavaScript Using Visual
Basic
Acrobat 7.0 provides a rich set of JavaScript programming interfaces that are designed to
be used from within the Acrobat environment. It also provides a mechanism (known as
JSObject) that allows external clients to access the same functionality from environments
such as Visual Basic.
This document gives you the information you need to get started using the extended
functionality of JavaScript from a Visual Basic programming environment. It provides a set
of examples to illustrate the key concepts.
What is JSObject?
In precise terms, JSObject is an interpretation layer between an OLE Automation client such
as a Visual Basic application and the JavaScript functionality provided by Acrobat. From a
programmer's point of view, the end result is that programming JSObject from a Visual
Basic environment is quite similar to programming in JavaScript using the Acrobat console.
Getting Started
The following steps get you set up to run the examples:
1. Install Acrobat 7.0 and Visual Basic .NET, since both are required for the examples in this
document.
2. Open a new Visual Basic.NET project. That gets you started with a blank form and
project workspace.
3. To access the Acrobat Automation APIs, including JSObject, you need to add a reference
to Acrobat's type library. From the UI, select Project > Add Reference, then the COM tab,
and from the list of available references, click on the item labeled “Adobe Acrobat 7.0
Type Library." Click Select. Click OK.
A Simple Example
This example describes the bare minimum required to display “Hello, Acrobat!” in Acrobat's
JavaScript console.
1. Bring up the source code window for this form by selecting View > Code from the UI.
2. Select (Form1 Events) from the selection box in the upper left corner of that window.
The selection box in the upper right shows all the functions available to the Form object.
3. Select Load from that box, which creates an empty function stub. The Form's Load
function is called when the Form is first displayed, so it's a good place to add the
initialization code.
This program uses some global variables for data that are required for its lifetime, and
initializes them in the Form1_Load routine.
features operate at the document level. There are some application-level features in
JavaScript (and therefore in JSObject), but they are of secondary interest. In practice, a
JSObject is always associated with a particular document. When working with a large
number of documents, you must structure your code such that a new JSObject is acquired
for each document, rather than creating a single JSObject to work on every document.
4. Now that a minimal user interface is set up, select View > Code from the main menu, and
add the following source code:
EXAMPLE 2 Working with annotations
OpenFileDialog1.ShowDialog()
path = OpenFileDialog1.FileName
pdDoc = CreateObject("AcroExch.PDDoc")
If pdDoc.Open(path) Then
jso = pdDoc.GetJSObject
If Not jso Is Nothing Then
pdDoc = Nothing
End Sub
The code in the Form_Load and FormName.Closed routines simply initializes and
shuts down the main Acrobat Automation interface. All the interesting work happens in the
Command button's click routine. The first few lines declare local variables and show the
Windows Open dialog, which allows the user to select a file to annotate. At that point, the
code opens the PDF's PDDoc object, and obtains a JSObject interface to that document.
Some standard Acrobat Automation methods are used to determine the size of the first
page in the document. These numbers are critical to achieving the correct layout, because
the PDF coordinate system is based in the lower-left corner of the page, but the annotation
will be anchored at the upper left corner of the page.
The lines following the "Create a new text annot" comment do exactly that, but
this block of code bears additional explanation. First of all, addAnnot looks as if it were a
method of JSObject, but the JavaScript reference shows that the method is associated with
the doc object. In that case, you might expect the syntax to be jso.doc.addAnnot—
however, jso is the Doc object, so jso.addAnnot is correct. All of the properties and
methods in the Doc object are used in this manner.
The second item of note is the use of annot.getProps and annot.setProps. The
Annot object is implemented with a separate properties object, meaning that you cannot
set the properties directly. For example, you cannot do the following:
annot = jso.AddAnnot
annot.Type = "Text"
annot.page = 0
...
Instead, you must obtain the Annot’s properties object using annot.getProps, and use
that object for read or write access. To save changes back to the original Annot, call
annot.setProps with the modified properties object, as in the original example.
Finally, note the use of JSObject's color property. This object defines several simple colors
such as red, green, and blue. In working with colors, you may need a greater range of colors
than is available through this object. Also, there is a performance hit associated with every
call to JSObject. To set colors more efficiently, you can use code such as the following,
which sets the annot's strokeColor to red directly, bypassing the color object.
dim color(0 to 3) as Variant
color(0) = "RGB"
color(1) = 1#
color(2) = 0#
color(3) = 0#
annot.strokeColor = color
You can use this technique anywhere a color array is needed as a parameter to a JSObject
routine. The example sets the colorspace to RGB, and specifies floating point values
ranging from 0 to 1 for red, green, and blue. Note the use of the # character following the
color values. These are required, since they tell Visual Basic that the array element should be
set to a floating point value, rather than an integer. It is also important to declare the array
as containing Variants, since it contains both strings and floating point values. The other
color spaces ("T", "G", "CMYK") have varying requirements for array length. Refer to the
Color object in the Acrobat JavaScript Scripting Reference for more details.
Spell-Checking a Document
Acrobat 7.0 includes a plug-in that can scan a document for spelling errors. This plug-in also
provides JavaScript methods that can be accessed using a JSObject. In this example, you’ll
start with the source code from Example 2, and make the following changes:
1. Add a List View control to the main form. Keep the default name ListView1 for the
control.
2. Replace the code in the existing Command1_Click routine with the following:
OpenFileDialog1.ShowDialog()
path = OpenFileDialog1.FileName
foundErr = False
pdDoc = CreateObject("AcroExch.PDDoc")
If pdDoc.Open(path) Then
jso = pdDoc.GetJSObject
If Not jso Is Nothing Then
count = jso.getPageNumWords(0)
For i = 0 To count - 1
word = jso.getPageNthWord(0, i)
If VarType(word) = vbString Then
result = jso.spell.checkWord(word)
If IsArray(result) Then
foundErr = True
ListView1.Items.Add (word & " is misspelled.")
ListView1.Items.Add ("Suggestions:")
For j = LBound(result) To UBound(result)
ListView1.Items.Add (result(j))
Next j
ListView1.Items.Add ("")
End If
End If
Next i
jso = Nothing
pdDoc.Close
pdDoc = Nothing
End Sub
In this example, note the use of the Spell object’s check method. According to the Acrobat
JavaScript Scripting Reference, this method takes a word as input, and returns a null object if
the word is found in the dictionary, or an array of suggested words if the word in not found.
As always, the safest approach when storing the return value of a JSObject method call is to
use a Variant. You can use the IsArray function to determine if the Variant is an array, and
act accordingly. In this simple example, if the program sees an array of suggested words, it
dumps them out to the List View control.