- Java Reference Guide
- Overview
- Table of Contents
- J2SE: Standard Java
- Java Windows NT Services
- Advanced J2SE
- J2SE 1.5.0: "Tiger"
- Java SE 6
- Core Computer Science Principles in Java (Data Structures)
- Annotations
- Java Generics
- Java New I/O
- Java Sound
- Java Applets
- JavaFX
- Java SE Threading
- Resource Management Using Semaphores
- Java Atomic Operations
- JavaTemplate Pages
- Executing Templates with the JtpExecutor
- Java Cryptography Extensions (JCE)
- Java Database Connectivity (JDBC) API
- Jakarta Commons - Net Class Library
- Jakarta Commons HttpClient
- Apache POI
- Regular Expressions
- JavaMail
- Cool Tools
- Tailer Library
- JTailer: A GUI Tailer
- Server-based Log Tailing
- JoSQL
- JavaSRC Cache
- JavaSRC Cache: A Cache Example
- Java Scheduler
- DeJar
- FindInJar
- JLayer
- SAH (HTML) Parser
- HTMLDocument – Part I
- HTMLDocument – Part II
- Launch4j
- RXML
- Reflection-based Swing Tools (RTools Swing)
- RTools - Annotations
- JLaunch: Launching External Processes
- JLaunch GUI
- PDF JavaBean
- Ganymed SSH-2 for Java
- Building an Really Simple Syndication (RSS) Java App
- Logging with Log4J
- Inside Swing
- Swing Components
- SwingX
- Swing Styled Documents
- Web Rendering in Java Swing Applications
- Java Look-and-Feel Graphics Repository
- Java Media Framework
- Quicktime for Java
- Media in Java Review 2008
- Graphs and Charts
- Holiday Special: Electronic Greeting Card
- Media Framework: Presenter Application
- Standard Widget Toolkit
- JFace
- Java Performance Tuning
- J2EE Performance Tuning
- Caches and Pools
- Java Caching System
- Java Compression and Decompression
- Obfuscating Java Applications
- Continuous Integration
- Load Testing
- Tomcat Clustering
- Enterprise Java Testing
- Automated Unit Testing with JUnit and Ant
- Unit Testing: Tips From The Trenches
- Custom Ant Tasks
- Extensible Markup Language (XML)
- Java Web Technologies
- Web Frameworks
- Struts 2
- Wicket
- JavaServer Faces
- Distributed Programming / RMI
- Servlet Filters
- Building a Robust Java Server
- J2EE: Enterprise Java
- Spring
- Java Design Patterns
- XDoclet
- Hibernate
- Project Backup
- J2EE Project: Hands-On
- Enterprise Java Beans (EJB) 3.0
- Disaster Recovery
- Java Management Extensions (JMX)
- Service-Oriented Architecture
- Web Services
- RESTful Web Services New
- Project: Building a Web Photo Gallery
- J2ME: Micro Java
- Specialized J2ME
- Optional Packages
- Other Java Technologies
- Derivatives and Competitors
- Java, Engineered for Integration
- Additional Resources
- The World of Java Tools
- Building Java Applications with Ant
- Managing Java Build Lifecycles with Maven
- Source Control with Subversion
- Inversion of Control and Dependency Injection
- Certification
- Roadmap: Becoming an Enterprise Java Developer
- Roadmap: Becoming an Enterprise Java Developer in 2007
- The Business of Enterprise Software
- JavaOne 2006
- JavaOne 2007
- JavaOne 2008 Wrap-Up
- JavaOne 2009 Wrap-Up
- How to Survive in a Turbulent Job Market
- How to Hire the Best Talent
- Cloud Computing
- Enterprise Java in 2008 and Beyond
- Predictions for 2018
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:
- Change directories to your destination directory, such as C:\lib or /home/yourname/lib/acrobat
- 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:
- Create a new Viewer instance
- Obtain an InputStream to your PDF file, which is accomplished by creating a FileInputStream that references the filename
- Pass the Viewer the InputStream by calling its setDocumentInputStream() method
- 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. Acrobat Viewer license agreement
And after you accept the license agreement it displays the familiar Acrobat user interface, shown in 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:
- Use the Viewer constructor that disallows certain commands, see the JavaDoc for more information
- 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.




Account Sign In
View your cart