Building PDF reports with JavaScript

Learn how to use the Report Object in Acrobat JavaScript to create simple text based PDF files.

By Thom Parker – May 18, 2006

 

by Thom Parker, Software Developer/Adventurer, WindJack Solutions, Inc.

Scope: Acrobat 5 and later
Category: Automating Acrobat
Skill Level: Intermediate
Prerequisites: Familiarity with Acrobat JavaScript

Acrobat JavaScript contains a range of functions and properties for analyzing both a PDF and the Acrobat environment. But how do you display the results of an analysis? The answer is the Report Object. This object creates simple text based PDF files. It’s not fancy, but it does provide a critical resource for presenting information in an easy to read and savable format.

Using the Report Object

Like many other objects in Acrobat JavaScript the Report Object comes with several useful features, but also some restrictions. Let’s look at the restrictions first. This object creates PDF files so it cannot be used in Reader at all. It can only be used in Acrobat Standard or Professional. The second restriction is that saving (with code) the newly created PDF can only be done be from a trusted context. In most cases this isn’t a problem since it’s usually the user that decides if the report should be saved or not.

The Report Object creates simple text reports. It doesn’t provide for the insertion of images or graphics, but is does allow specifying text size and color, and formatting the text with divider lines and indenting.

The sequence of actions for creating a report starts with constructing a new Report Object. It is at this point that the page size and page margins are set. The default is the standard letter size with a 1⁄2 inch margin on all sides, but these parameters can be set to any value. This new object is then used to write lines of text and formatting options to a report buffer. When the report is complete, the actual PDF report is created by calling the Report Object’s open() function.

As an example of this sequence, the following code creates a report of all the form fields on the current document as shown in Figure 1 (download the sample file below)

Example File: PDF Creation Report
Download PDF [PDF: 38 KB]

// Report is created in a wide format with the default margin 
var oReport = new Report([0,459, 659, 0]); 

// Set up Report Title 
oReport.style = "NoteTitle"; 
oReport.size = 2.5; 
oReport.writeText("Form Field Report for:"); 
oReport.indent(); 
oReport.writeText(this.documentFileName); 
oReport.outdent(); 
oReport.divide(); 

// Now build a summary of field info 
oReport.size = 1.2; 
oReport.style = "DefaultNoteText"; 

var fieldTypes = {}; // Create new Object to hold field summary 
oReport.writeText("Form Fields by type: "); 
oReport.indent(); 
for(var i=0;i<this.numFields;i++) {
    // Loop through all fields on document 
    var fldName = this.getNthFieldName(i); 
    var oFld = this.getField(fldName); 
    if(fieldTypes[oFld.type] == null) fieldTypes[oFld.type] = [fldName]; 
        else fieldTypes[oFld.type].push(fldName); 
} 
// Write Field Report 
for(var ftype in fieldTypes) { 
    var fldList = fieldTypes[ftype]; 
    oReport.writeText(ftype + ": " + fldList.length + " fields"); 
    oReport.indent(); 
    for(var i=0;i<fldList.length;i++) oReport.writeText(fldList[i]); oReport.outdent(); 
} 
// Open report into a PDF oReport.open("Form Field Report");


Figure 1 – Output of Form Field Report Script


Note that the main body of the report is created in a two step process. First, the form field names are collected into an object that sorts them by field type. Then, after data collection is complete, the compiled results are written to the report. This two step technique allows both organizing the collected data into a structure that’s convenient for the desired output format and displaying summary information before the details.

This report could be expanded to include other form field parameters such as whether the field is hidden or visible, the pages the fields appear on, or any other field property of interest. By the same token, any information available through Acrobat JavaScript can be used to create reports.



Related topics:

JavaScript

Top Searches:


0 comments

Comments for this tutorial are now closed.

Comments for this tutorial are now closed.