Export Swing components to PDF - Java World
Justin Henine-Hardenne  |  by www.javaworld.com. All rights reserved. 11.12 | 18:34

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 Figure 1. A Swing application with a JTable that is printed to PDF two different ways.

Click on thumbnail to view full-sized 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 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:

  • For more on JFreeChart, read "Chart a New Course with JFreeChart," Erik Swenson (JavaWorld, December 2002):
  • For more articles on Java development tools, browse JavaWorld's Development Tools Research Center:
  • For more articles on Swing, browse through the articles in JavaWorld's AWT/Swing Research Center:
  • Keep up with what's new at JavaWorld!

  • Read more on by www.javaworld.com. All rights reserved.
    Keywords: Development Tools, Research Center
    Related news
    Post comments
    Name
    Place
    8 + 4 =
    Comments