Home > Guides > Programming > Java

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

PDF JavaBean

Last updated Nov 30, 2007.

If you have ever wanted to display PDF files in your Java applications, Adobe provides a rich JavaBean that does just that. It provides all of the features that you’ll be used to when using Acrobat Reader, including zoom, print, select text, find text, and so forth. And being a JavaBean, you can simply create a container for it and add it to your application.

The first step is to download the JavaBean from here. I downloaded the platform independent JAR file, bean.jar, but you can opt to download an operating system specific installation if you wish. If you download the bean.jar file, you’ll need to extract it to a local directory on your computer, which can be done as follows:

  1. Change directories to your destination directory, such as C:\lib or /home/yourname/lib/acrobat
  2. Execute the following command
    jar xf <path-to-jarfile>/bean.jar *

This will create a set of files and directories, including documentation and sample files. There are two files that you need to add to your CLASSPATH:

  • acrobat.jar
  • MRJToolkitStubs.zip

For example, on Windows set your CLASSPATH as follows:

set CLASSPATH=.;c:\lib\acrobat\acrobat.jar;c:\lib\acrobat\MRJToolkitStubs.zip

Or on Linux:

CLASSPATH=.:/home/yourname/lib/acrobat/acrobat.jar:/home/yourname/lib/acrobat/MRJToolkitStubs.zip ; export CLASSPATH

With this CLASSPATH setup, you can create the application shown in listing 1.

Listing 1. PDFDocument.java

package com.javasrc.studycenter;

import com.adobe.acrobat.Viewer;

import javax.swing.*;
import java.awt.*;
import java.io.FileInputStream;
import java.util.*;
import java.util.List;

/**
 * Class to display a PDF document in a JFrame
 */
public class PDFDocument extends JPanel
{
  private Viewer viewer;

  public PDFDocument( String filename )
  {
    // Set the layout for this panel
    this.setLayout( new BorderLayout() );

    // Create the viewer
    try
    {
      viewer = new Viewer();
      FileInputStream fis = new FileInputStream( filename );
      viewer.setDocumentInputStream( fis );
      this.add( viewer, BorderLayout.CENTER );
      viewer.activate();

      String page1 = viewer.getTextForPage( 0 );
      System.out.println( "page1: \n" + page1 );
    }
    catch( Exception e )
    {
      e.printStackTrace();
    }
  }

  public static void main( String[] args )
  {
    if( args.length == 0 )
    {
      System.out.println( "Usage: PDFDocument <filename>" );
      System.exit( 0 );
    }

    String filename = args[ 0 ];
    PDFDocument pdf = new PDFDocument( filename );

    // Create our frame
    JFrame frame = new JFrame( "PDF Viewer" );
    frame.setSize( 1024, 768 );
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation( d.width/2 - 512, d.height/2 - 384 );
    frame.getContentPane().add( pdf );
    frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
    frame.setVisible( true );
  }
}

The PDFDocument class accepts a PDF filename and displays it in a JFrame. The main() method creates a new JFrame, resizes it to 1024x768, adds the PDFDocument class to its CENTER, and then uses the Toolkit class to obtain the screen size and center the frame on the screen.

The PDFDocument class’s constructor uses the com.adobe.acrobat.Viewer class to perform all of the work in loading the PDF document and displaying it in the PDFDocument’s JPanel. You need to implement the following steps to use the Viewer class:

  1. Create a new Viewer instance
  2. Obtain an InputStream to your PDF file, which is accomplished by creating a FileInputStream that references the filename
  3. Pass the Viewer the InputStream by calling its setDocumentInputStream() method
  4. Call the Viewer class’s activate() method to ask it to parse the PDF document and build its user interface

When you run the PDFDocument class, it first displays a license agreement from Adobe (which is displayed by the Viewer class when you invoke its activate() method), shown in figure 1.

Figure 1

Figure 1. Acrobat Viewer license agreement

And after you accept the license agreement it displays the familiar Acrobat user interface, shown in figure 2.

Figure 2

Figure 2. PDFDocument class showing a PDF document

The user interface should look familiar to you. And because it is a JavaBean, you can embed it anywhere in your AWT or Swing application (it does not need to be displayed alone.) If you want finer grained control over the appearance of the user interface as well as of the commands available (such as select text), you can do one of two things:

  1. Use the Viewer constructor that disallows certain commands, see the JavaDoc for more information
  2. Instead of invoking activate() you can invoke activateWithoutBars() and then control it from your application. The bean provides a method called execMenuItem() to send it commands and control its behavior, such as allowing you to zoom, change pages, and so forth. Again there’s more information in the JavaDoc that accompanies the archive that you downloaded (bean.jar)

The last thing that I added to the PDFDocument constructor, just to illustrate how to do it, was to obtain the readable text from the first page of the document by calling getTextForPage() and displaying it to the standard output device. If you want to parse through the text of the PDF document, you can do so by using the getTextForPage() method and iterating through all of the pages. You can obtain the page count by calling getPageCount() and then iterate from page 0 to the page count minus 1. Unfortunately it does not include any formatting information (such as new lines), but it does provide you an interface to programmatically reading a PDF document.

Summary

Parsing and displaying a PDF document manually is an arduous task. If you want some help you can review the jPod intarsys PDF library for an API to PDF documents, but regardless, it is not trivial. Luckily Adobe has built a JavaBean that you can easily integrate into your AWT and Swing applications that handles all of the work for you in a handful of lines of code.

Discussions

Read and display the table in the document
Posted Nov 12, 2008 06:01 AM by StrongHead
1 Replies
Correction
Posted Nov 4, 2008 06:09 PM by youssef.mohammed
1 Replies
Instead of synchronising getInstance
Posted Nov 3, 2008 05:42 AM by grahamkelly
1 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Dustin SullivanIf You Are New to Java Programming...
By Dustin SullivanJune 2, 20092 Comments

We recently sat down with several top Java developers to talk about that state of the language as we approach this year's JavaOne.  As we were wrapping up, we threw one last question at them out of curiosity, and we thought you'd like to see what some of them said.

Steven HainesOracle Buys Sun of $7.4B
By Steven HainesApril 20, 2009 No Comments

In a stunning turn of events, Oracle steps in and buys Sun amist the breakdown of IBM's attempt to acquire Sun.

Steven HainesIBM in talks to buy Sun Microsystems for at least $6.5B
By Steven HainesMarch 18, 2009 No Comments

Reuters reported this morning that IBM is in talks to buy Sun Microsystems, which could "bolster their computer server products against rivals such as Hewlett-Packard Co."

See More Blogs

Informit Network