Wednesday, April 30, 2008

Add New Acrobat Features with Startup JavaScripts

When Acrobat starts up, it runs any JavaScripts it finds in either the system-level JavaScripts folder or the user-level JavaScripts folder. The locations of these folders are given shortly. You might need to create some of these folders if you can't find them.

Use startup JavaScripts to add menu items to Acrobat or to set global JavaScript variables.] Acrobat stores these persistent global variables in a file named glob.js.

These JavaScripts even enable you to add features to the free Reader, although Reader won't perform the more powerful commands. Our various JavaScript hacks all work with Reader. JavaScripts are also platform-independent, so our JavaScript hacks all run on Windows, Mac, and Linux.

Customize Acrobat Using JavaScript

Create custom Acrobat menu items and batch processing scripts.

Acrobat can do most of the things that you need. Yet, there's always something you wish it did a little differently. Acrobat enables you to add custom features using plain-text JavaScripts. These scripts can add menu items to Acrobat's menus or add tailored sequences to Acrobat's batch processing.

Acrobat JavaScript builds on the language core familiar to web developers, but its document object model is completely different from the DOM used by web browsers. Acrobat's JavaScript objects are documented in Technical Note 5186: Acrobat JavaScript Object Specification. Access it online from http://partners.adobe.com/asn/developer/pdfs/tn/5186AcroJS.pdf. Another useful document is the Acrobat JavaScript Scripting Guide from http://partners.adobe.com/asn/acrobat/sdk/public/docs/AcroJSGuide.pdf.

The JavaScript Debugger (Acrobat 6 Pro) or Console (Acrobat 5) is the place to test new ideas. Open it by selecting Advanced > JavaScript > Debugger . . . (Acrobat 6 Pro) or Tools > JavaScript > Console . . . (Acrobat 5).

The Code

In this example, the Perl script will use Acrobat to read annotation (e.g., sticky notes) data from the currently open PDF. The script will format this data using HTML and then output it to stdout.
Download the script here (SummarizeComments.pl)


Friday, April 25, 2008

How to Install PERL

Depending on your tastes or requirements, you might want to use the Perl scripting language instead of Visual Basic to program Acrobat. Perl can access the same Acrobat OLE interface used by Visual Basic to manipulate PDFs. Perl is well documented, is widely supported, and has been extended with an impressive collection of modules. A Perl installer for Windows is freely available from ActiveState.

We'll describe how to install the ActivePerl package from ActiveState, and then we'll use an example to show how to access Acrobat's OLE interface using Perl.

Acrobat OLE documentation comes with the Acrobat SDK. Look for IACOverview.pdf and IACReference.pdf. Acrobat Distiller also has an OLE interface. It is documented in DistillerAPIReference.pdf.

Install Perl on Windows
The ActivePerl installer for Windows is freely available from http://www.ActiveState.com/Products/ActivePerl/. Download and install. It comes with excellent documentation, which you can access by selecting Start Programs ActiveState ActivePerl 5.8 Documentation.

ActivePerl also includes the OLE Browser, which enables you to browse the OLE servers available on your machine (Start Programs ActiveState ActivePerl 5.8 OLE-Browser). The OLE Browser is an HTML file that must be opened in Internet Explorer to work properly.

Running the Code

Open a PDF in Acrobat In Word, run the macro by selecting Tools Macro Macros . . . SummarizeComments and then clicking Run. After a few seconds, a new Word document will appear. It will list all the comments that readers have added to each page of the currently visible PDF.

This script demonstrates the typical process of drilling down through layers of PDF objects to find desired information. Here is a simplified sketch of the layers:


app
The currently running Acrobat program. Use the app to alter the user interface or Acrobat's preferences.

avdoc
The PDF currently displayed in Acrobat. Use the avdoc to change how the PDF appears in the viewer or to print pages.

pddoc
Represents the underlying PDF document. Use the pddoc to access or manipulate the PDF's pages or metadata.

pdpage
Represents the underlying PDF page. Use the pdpage to access or manipulate a page's annotations, its rotation, or its cropping.

These OLE objects closely resemble the objects exposed by the Acrobat API. The API gives you much more power, however.

Drive Acrobat using VB or Microsoft Word's Visual Basic for Applications (VBA)

Adobe Acrobat's OLE interface enables you to access or manipulate PDFs from a freestanding Visual Basic script or from another application, such as Word. You can also use Acrobat's OLE interface to render a PDF inside your own program's window. The Acrobat SDK [Hack #98] comes with a number of Visual Basic examples under the InterAppCommunicationSupport directory. The SDK also includes OLE interface documentation. Look for IACOverview.pdf and IACReference.pdf. These OLE features do not work with the free Reader; you must own Acrobat.

Acrobat Distiller also has an OLE interface. It is documented in DistillerAPIReference.pdf, which comes with the full Acrobat SDK.


The following example shows how easily you can work with PDFs using Acrobat OLE. It is a Word macro that scans the currently open PDF document for readers' annotations (e.g., sticky notes). It creates a new Word document and then builds a summary of these annotation comments.

The Code
To add this macro to Word, select Tools > Macro > Macros . . . , type in the macro name SummarizeComments, and click Create. Word will open a text editor where you can enter the code. Save, and then test. You can download this code from http://www.pdfhacks.com/summarize.

VBA code for summarizing comments

Sub SummarizeComments( )

Dim app As Object

Set app = CreateObject("AcroExch.App")

If (0 < newdoc =" Documents.Add(DocumentType:=" newdocrange =" NewDoc.Range" found_notes_b =" False" avdoc =" app.GetActiveDoc" pddoc =" avdoc.GetPDDoc" num_pages =" pddoc.GetNumPages" ii =" 0" pdpage =" pddoc.AcquirePage(ii)" page_head_b =" False" num_annots =" pdpage.GetNumAnnots" jj =" 0" annot =" pdpage.GetAnnot(jj)"> "" And _

annot.GetSubtype <> "Popup") Then



If (page_head_b = False) Then ' output the page number

NewDocRange.Collapse wdCollapseEnd

NewDocRange.Text = "Page: " & (ii + 1) & vbCr

NewDocRange.Bold = True

NewDocRange.ParagraphFormat.LineUnitBefore = 1

page_head_b = True

End If



' output the annotation title and format it a little

NewDocRange.Collapse wdCollapseEnd

NewDocRange.Text = annot.GetTitle & vbCr

NewDocRange.Italic = True

NewDocRange.Font.Size = NewDocRange.Font.Size - 1

NewDocRange.ParagraphFormat.LineUnitBefore = 0.6



' output the note text and format it a little

NewDocRange.Collapse wdCollapseEnd

NewDocRange.Text = annot.GetContents & vbCr

NewDocRange.Font.Size = NewDocRange.Font.Size - 2



found_notes_b = True

End If

Next jj

End If

Next ii



If (Not found_notes_b) Then

NewDocRange.Collapse wdCollapseEnd

NewDocRange.Text = "No Notes Found in PDF" & vbCr

NewDocRange.Bold = True

End If

End If

End Sub

Monday, April 21, 2008

Convert Microsoft Office Documents to PDF

If you have Acrobat 6 and Microsoft Word, you can use Acrobat's preconfigured Open All batch sequence to convert Word documents into PDFs hands-free. As the name suggests, you actually can use the Open All batch sequence on any kind of file that Acrobat knows how to handle, including bitmap and PostScript files. Acrobat 5 also has an Open All batch sequence, but it does not handle as many file types as Acrobat 6 does.

To merge a number of Word documents into a single PDF with Acrobat 6, use the File Create PDF From Multiple Files . . . feature instead.

First, you must configure Acrobat 6 to create the kind of PDF you desire. Do this using the Acrobat preferences, located at Edit > Preferences > General . . . > Convert to PDF. Select Microsoft Office and click Settings . . . , and a dialog opens.

In Acrobat 6, start the Open All batch sequence by selecting Advanced > Batch > Processing . . . Open All and clicking Run Sequence. In Acrobat 5, start the Open All batch sequence by selecting File Batch Processing Open All. Click OK to close the confirmation dialog (if necessary), and a file selector will open. Change Files of Type to All Files, select one or more input files, and then click Select. Acrobat will create one PDF for each input document. Acrobat 5 can't process Word documents this way, but it can handle bitmap images.