Export Swing components to PDF

Use JFreeChart and iText to draw charts

By Bruno Lowagie

JavaWorld|Dec 9, 2006 12:00 AM
PT

Suppose you've written an application with a GUI using Swing components such as JTable or JTextPane. All these components are derived from the abstract class javax.swing.JComponent, which includes the print(Graphics g) method: You can use this method to let the Swing component print itself to iText's PdfGraphics2D object.

Figure 1 shows a simple Java application with a JFrame. It contains a JTable found in Sun's Java tutorial on Swing components. If you click the first button, the contents of the table are added to a PDF using createGraphicsShapes() (the upper PDF in the screenshot). If you click the second button, the table is added using createGraphics() (the lower PDF, using the standard Type 1 font Helvetica). Notice the subtle differences between the fonts used for both variants.

Figure 1. A Swing application with a JTable that is printed to PDF two different ways. Click on thumbnail to view full-sized image.

If you run this example, try changing the content of the JTable; the changes are reflected in the PDF. If you select a row, the background of the row is shown in a different color in the Java applications as well as in the PDF.

The next example was posted to the iText mailing list by Bill Ensley (bearprinting.com), one of the more experienced iText users on the mailing list. It's a simple text editor that allows you to write text in a JTextPane and print it to PDF.

Figure 2 shows this application in action.

Figure 2. A simple editor with a JTextPane that is drawn onto a PDF file. Click on thumbnail to view full-sized image.

The code is a bit more complex than the JTable example. This example performs an affine transformation before the content of the JTextPane is painted:

JasperReports, a free Java reporting tool from JasperSoft, allows you to deliver content onto the screen; to the printer; or into PDF, HTML, XLS, CSV, and XML files. If you choose to generate PDF, iText's PdfGraphics2D object is used behind the scenes.

ICEbrowser is a product from ICEsoft. ICEbrowser parses and lays out advanced Web content (XML/HTML/CSS/JS); PDF is generated by rendering the parsed documents to the PdfGraphics2D object.

It's not my intention to make a complete list of products that use iText. The main purpose of these two examples is to answer the following question: Can I build iText into my commercial product? Lots of people think open source is the opposite of commercial, but that's a misunderstanding. It's not because iText is FOSS that it can only be used in other free products. It's not because iText is free that it isn't a "commercial" product. As long as you respect the license, you can use iText in your closed-source or proprietary software.

Another useful aspect of iText's Graphics2D functionality is that it opens the door to using iText in combination with other libraries with graphical output—for instance, Apache Batik, a library that is able to parse SVG; or JFreeChart, a library that will be introduced in the next section.

Drawing charts with JFreeChart

Suppose you need to make charts showing demographic information. You take the student population of the Technological University of Foobar and graph the number of students per continent.

To make these charts, you'll combine iText with JFreeChart, an interesting library developed by David Gilbert and Thomas Morgner. The Website jfree.org explains that JFreeChart is "a free Java class library for generating charts, including pie charts (2D and 3D), bar charts (regular and stacked, with an optional 3D effect), line and area charts, scatter plots and bubble charts, time series, high/low/open/close charts and candle stick charts, combination charts, Pareto charts, Gantt charts, wind plots, meter charts and symbol charts, and wafer map charts."

These charts can be rendered on an AWT (Abstract Window Toolkit) or Swing component, they can be exported to JPEG or PNG, and you can combine JFreeChart with Apache Batik to produce SVG or with iText to produce PDF.

Figure 3 shows PDFs with a pie chart and a bar chart created using JFreeChart and iText. In JFreeChart, you construct a JFreeChart object using the ChartFactory. One of the parameters passed to one of the methods to create the chart is a dataset object.

Figure 3. Foobar statistics represented in a pie chart and a bar chart. Click on thumbnail to view full-sized image.