Printing in Java, Part 3
Jean-Pierre Dubé introduces the print framework that works on top of the Java Print API
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.
Printing in Java: Read the whole series!
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:
- Be easy to use.
- Provide the high-level functionality required to efficiently render pages.
- 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.
- Provide a structure that clearly defines each component involved in creating documents (document, page, paragraph, and so
- 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.
- Provide a print-preview facility. The Java Print API has no support for previewing the output before you print.
- 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.
- 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.
- 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
- Support graphics primitives such as rectangles, circles, and lines.
- Support GIF and JPEG image types.
- Support headers and footers. These can be set at the document or the page level, and the first page can feature a different
- 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.
- Support sticky dimensions, with which you can set a component's width and/or height to the parent container's width and/or height.
- Support tables, though their implementation will be limited.
In addition, all print objects are containers in the print framework.