Adobe's Portable Document Format (PDF) is a popular choice for exchanging documents over the Internet, but how do you access and present PDF files in your Java applications? Jeff Friesen introduces this month's Open Source Java Project: a PDF rendering tool that you can easily integrate with your Java applications.
Java-based products such as Bruno Lowagie's iText make it easy for Java programs to create PDF documents. For example, a servlet could extract product data from a database, and then use iText to dynamically create a PDF-based product catalog based on this data. iText and products like it aren't designed to render a PDF document's content, however.
A few years back, Sun Labs researchers developed the all-Java PDF Renderer project because they needed a PDF viewer for content created by OpenOffice. When this project was no longer needed, Sun Labs offered PDF Renderer to Josh Marinacci and Richard Bair (of SwingLabs) who set out to get the project open sourced. Tom Oke signed on to head up future work on the project, and Josh announced the release of the open source PDF Renderer project in December 2007.
In this installment in the Open Source Java Projects series, I'll introduce you to the SwingLabs PDF Renderer. After you've downloaded the project source and run a short demo, we'll explore the PDF Renderer API. Following application exercises in rendering PDF content to your application screen and sending it to a printer, I'll wrap up with a more complex example that demonstrates PDF Render's usefulness.
|This project's license|
|PDF Renderer adheres to the GNU Lesser General Public License (LGPL). See the Resources section to learn more about this open source license.|
Get started with PDF Renderer
The pdf-renderer project, hosted on Java.net, introduces PDF Renderer and provides access to its executable and source code. Point your browser to the site's "Documents & files" section and download the most recent version of PDF Renderer's distribution archive. For example, I downloaded
The distribution archive contains a JAR file that stores the API's class files. The archive also stores the class files to a PDF viewer demo that demonstrates the API's usefulness for viewing arbitrary PDF documents. You can run this demo at the command line by specifying the following command (assuming that the current directory contains
java -jar PDFRenderer-2008_05_18.jar
Figure 1 shows the demo presenting the first page of a sample PDF document. The left pane of the demo's split-pane GUI reveals this document's thumbnail images, which you can click to select the page that appears in the right pane. The right pane presents the current page; you can zoom into this page by selecting the toolbar's magnifying tool and selecting a rectangular area.
The distribution archive also contains Javadoc-based documentation, which describes PDF Renderer's API. This documentation is separately available in its own archive --
PDFRenderer-2008_05_18-javadoc.zip, for example. Also in its own archive is the source code to the API and demo. For the previously mentioned distribution archive,
PDFRenderer-2008_05_18-src.zip is the equivalent source archive.