Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Export Swing components to PDF

Use JFreeChart and iText to draw charts

  • Print
  • Feedback

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.

(Note: This article excerpts Chapter 12, "Drawing to Java Graphics2D," from iText in Action, Bruno Lowagie (Manning Publications, December 2006; ISBN: 1932394796): http://www.manning.com/lowagie.)

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 code to achieve this is amazingly simple:

 /* chapter12/MyJTable.java */
public void createPdf(boolean shapes) {
   Document document = new Document();
   try {
      PdfWriter writer;
      if (shapes)
         writer = PdfWriter.getInstance(document,
            new FileOutputStream("my_jtable_shapes.pdf"));
      else
         writer = PdfWriter.getInstance(document,
            new FileOutputStream("my_jtable_fonts.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      PdfTemplate tp = cb.createTemplate(500, 500);
      Graphics2D g2;
      if (shapes)
         g2 = tp.createGraphicsShapes(500, 500);
      else
         g2 = tp.createGraphics(500, 500);
      table.print(g2);
      g2.dispose();
      cb.addTemplate(tp, 30, 300);
      } catch (Exception e) {
      System.err.println(e.getMessage());
   }
   document.close();
}

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:

 /* chapter12/JTextPaneToPdf.java */
Graphics2D g2 = cb.createGraphics(612, 792, mapper, true, .95f);
AffineTransform at = new AffineTransform();
at.translate(convertToPixels(20), convertToPixels(20));
at.scale(pixelToPoint, pixelToPoint);
g2.transform(at);
g2.setColor(Color.WHITE);
g2.fill(ta.getBounds());
Rectangle alloc = getVisibleEditorRect(ta);
ta.getUI().getRootView(ta).paint(g2, alloc);
g2.setColor(Color.BLACK);
g2.draw(ta.getBounds());
g2.dispose();

Numerous applications use iText this way. Let me pick two examples; one free/open source software (FOSS) product and one proprietary product:

  • Print
  • Feedback

Resources