Open source Java projects: SwingLabs PDF Renderer

View and render PDF content from your Java programs

1 2 3 4 5 Page 2
Page 2 of 5

PDF Renderer's API

PDF Renderer presents an API that's organized into several packages, with com.sun.pdfview serving as the main package. This package's PDFFile class is the entry point into the API. The following code fragment shows you how to use this class's solitary public PDFFile(ByteBuffer buf) constructor to create a PDFFile instance initialized to a specific PDF document:

RandomAccessFile raf = new RandomAccessFile (new File ("sample.pdf"), "r");
FileChannel fc = raf.getChannel ();
ByteBuffer buf = (FileChannel.MapMode.READ_ONLY, 0, fc.size ());
PDFFile pdfFile = new PDFFile (buf);

According to PDFFile's Javadoc, the constructor's java.nio.ByteBuffer argument is derived from a instance (via a java.nio.channels.FileChannel intermediary). The constructor throws if it cannot find the document's cross-reference table or trailer dictionary, and com.sun.pdfview.PDFParseException if it has trouble parsing these items.

Obtaining document information

The PDFFile class presents several methods that return information about the PDF document. For example, public String getVersionString() returns the document's version number as a string, whereas public int getMajorVersion() and public int getMinorVersion() return the version number's major and minor components as integers.

Version checking
Version methods are useful for determining whether PDF Renderer can parse certain documents -- for instance, whether a document was created with a more recent version of Acrobat and contains features that cannot be parsed or rendered by PDF Renderer.

The public boolean isPrintable() and public boolean isSaveable() methods are useful for determining whether the document's owner has given permission to print or save a copy of the document. It's a good idea to respect the owner's wishes in this regard by disabling any printing and/or saving features in your own code should these methods return false.

Another useful method is public OutlineNode getOutline(), which returns the root node of the PDF document's outline hierarchy as a com.sun.pdfview.OutlineNode instance. Because OutlineNode subclasses javax.swing.tree.DefaultMutableTreeNode, you can invoke inherited methods such as public Enumeration preorderEnumeration() to enumerate this hierarchy.

Listing 1 presents the source code to an application that invokes these methods to obtain the version number, print and save status, and outline hierarchy of a PDF document identified via a command-line argument.

Listing 1.



import java.nio.*;
import java.nio.channels.*;

import java.util.*;

import javax.swing.tree.*;

import com.sun.pdfview.*;

public class PDFInfo
   public static void main (String [] args) throws IOException
      if (args.length != 1)
          System.err.println ("usage: java PDFInfo pdfspec");

      RandomAccessFile raf = new RandomAccessFile (new File (args [0]), "r");
      FileChannel fc = raf.getChannel ();
      ByteBuffer buf = (FileChannel.MapMode.READ_ONLY, 0, fc.size ());
      PDFFile pdfFile = new PDFFile (buf);

      System.out.println ("Major version = "+pdfFile.getMajorVersion ());
      System.out.println ("Minor version = "+pdfFile.getMinorVersion ());
      System.out.println ("Version string = "+pdfFile.getVersionString ()+"\n");

      System.out.println ("Is printable = "+pdfFile.isPrintable ());
      System.out.println ("Is saveable = "+pdfFile.isSaveable ()+"\n");

      OutlineNode oln = pdfFile.getOutline ();
      if (oln != null)
          System.out.println ("Outline\n");

          Enumeration e = oln.preorderEnumeration ();
          while (e.hasMoreElements ())
             DefaultMutableTreeNode node;
             node = (DefaultMutableTreeNode) e.nextElement ();
             System.out.println (node);

Invoke the following command to compile

javac -cp PDFRenderer-2008_05_18.jar

Figure 1 revealed some content from a PDF document stored in langspec-3.0.pdf. Assuming a Windows platform, invoke the following command to obtain and output this document's version number and more:

java -cp PDFRenderer-2008_05_18.jar;. PDFInfo langspec-3.0.pdf

PDFInfo generates the output shown in Listing 2, which I've abbreviated. <top> is the textual value assigned to the outline hierarchy's root node and is technically not part of the hierarchy.

Listing 2. Output of PDFInfo

Major version = 1
Minor version = 3
Version string = 1.3

Is printable = true
Is saveable = true


The Java? Language Specification
Preface to the Second Edition
Preface to the Third Edition
1.1 Example Programs
1.2 Notation
1.3 Relationship to Predefined Classes and Interfaces
1.4 References
2.1 Context-Free Grammars
2.2 The Lexical Grammar
2.3 The Syntactic Grammar
2.4 Grammar Notation
18.1 The Grammar of the Java Programming Language
1 2 3 4 5 Page 2
Page 2 of 5