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

Printing in Java, Part 3

Jean-Pierre Dubé introduces the print framework that works on top of the Java Print API

  • Print
  • Feedback
In this third part of a five-part series on printing in Java, I will explain the design of the print framework. This framework will work on top of the API to ease the burden of creating printed output. With it, you will be able to create pages with running headers/footers, and insert paragraphs, images, and tables. The coding phase will begin in Part 4 and continue into Part 5. We have a lot of ground to cover, so let's start.

Goals of the print framework



In Parts 1 and 2, you had the opportunity to examine in detail the Java Print API. With this knowledge, you are now able to evaluate its strengths and weaknesses and build a framework on top of it. That framework will compensate for the lack of high-level features in the Java Print API.

The print framework will:

  1. Be easy to use.
  2. Provide the high-level functionality required to efficiently render pages.
  3. Loosely couple with the Print API. Because the framework has no direct ties to the Java Print API, it will allow greater support for other output formats, such as PDF or HTML.
  4. Provide a structure that clearly defines each component involved in creating documents (document, page, paragraph, and so on).
  5. Use an abstract measurement system. Developers will thus be able work with the measurement system of their choice or even create their own. The framework will provide three default units of measurements: inches, centimeters, and points.
  6. Provide a print-preview facility. The Java Print API has no support for previewing the output before you print.
  7. Support a standard and portable page-setup dialog. As mentioned in Part 2, even though Java has been categorized as a WORA (Write Once, Run Anywhere) language, its functionality varies from platform to platform. The print framework will provide one page-setup dialog that will be uniform across all platforms.
  8. Offer export features. Although the framework will support several output formats (PDF, HTML, Postscript), their implementation is beyond the scope of this series. I leave the implementation up to you.
  9. Offer text handling, which is of primary importance. The framework will provide all the functionality needed to effectively render text, including right justification, left justification, full justification, and support for the AttributedString class.
  10. Support graphics primitives such as rectangles, circles, and lines.
  11. Support GIF and JPEG image types.
  12. Support headers and footers. These can be set at the document or the page level, and the first page can feature a different header/footer.
  13. Support sticky position, which easily sticks a component to a specific location. As an example, let's assume that you want to center an object in a page. Instead of doing all the math yourself, you can set the vertical and horizontal sticky value to a CENTER value; the framework centers the object within its parent container -- in this case, the page. Since all print objects are containers, the sticky values would be applied within the boundaries of the parent object.
  14. Support sticky dimensions, with which you can set a component's width and/or height to the parent container's width and/or height.
  15. Support tables, though their implementation will be limited.


In addition, all print objects are containers in the print framework.

  • Print
  • Feedback

Resources