Creating a simple application

This example provides the minimum code to display “Hello, Acrobat!” in the Acrobat JavaScript console.

To set up and run the “Hello, Acrobat!” example:

1.Open the source code window for the default form by clicking View > Code.

2.Select (Form1 Events) from the selection box in the upper left corner of that window.

The selection box in the upper right corner now shows all the functions available to the Form1 object.

3.Select Load from the functions selection box. This creates an empty function stub. The Form1 Load function is called when Form1 is first displayed, so this is a good place to add the initialization code.

4.Add the following code to define some global variables before the subroutine.

   Dim gApp As Acrobat.CAcroApp

   Dim gPDDoc As Acrobat.CAcroPDDoc

   Dim jso As Object

5.Add the following code to the private Form1_Load subroutine.

      gApp = CreateObject("AcroExch.App")

      gPDDoc = CreateObject("AcroExch.PDDoc")

      If gPDDoc.Open("c:\example.pdf") Then

         jso = gPDDoc.GetJSObject

         jso.console.Show

         jso.console.Clear

         jso.console.println ("Hello, Acrobat!")

         gApp.Show

      End If

6.Create a file called example.pdf at the root level of the C: drive.

7.Save and run the project.

When you run the application, Acrobat is launched, Form1 is displayed, and the JavaScript Debugger window is opened, displaying “Hello, Acrobat!”.

   Dim gApp As Acrobat.CAcroApp

   Dim gPDDoc As Acrobat.CAcroPDDoc

   Dim jso As Object

   

   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
         Handles Me.Load

      gApp = CreateObject("AcroExch.App")

      gPDDoc = CreateObject("AcroExch.PDDoc")

      If gPDDoc.Open("c:\example.pdf") Then

         jso = gPDDoc.GetJSObject

         jso.console.Show

         jso.console.Clear

         jso.console.println ("Hello, Acrobat!")

         gApp.Show

      End If

   End Sub

The Visual Basic program attaches to the Acrobat automation interface using the CreateObject call, and then shows the main window using the App object's Show command.

You may have a few questions after studying the code. For example, why is jso declared as an Object, while gApp and gPDDoc are declared as types found in the Acrobat type library? Is there a real type for JSObject?

The answer is no, JSObject does not appear in the type library, except in the context of the CAcroPDDoc.GetJSObject call. The COM interface used to export JavaScript functionality through JSObject is known as an IDispatch interface, which in Visual Basic is more commonly known simply as an “Object” type. This means that the methods available to the programmer are not particularly well-defined. For example, if you replace the call to

   jso.console.clear

with

   jso.ThisCantPossiblyCompileCanIt("Yes it can!")

the compiler compiles the code, but fails at run time. Visual Basic has no type information for JSObject, so Visual Basic does not know if a particular call is syntactically valid until run-time, and will compile any function call to a JSObject. For that reason, you must rely on the documentation to know what functionality is available through the JSObject interface. For details, see the JavaScript for Acrobat API Reference.

You may also wonder why it is necessary to open a PDDoc before creating a JSObject. Running the program shows that no document appears onscreen, and suggests that using the JavaScript console should be possible without a PDDoc. However, JSObject is designed to work closely with a particular document, as most of the available 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 so that a new JSObject is acquired for each document, rather than creating a single JSObject to work on every document.