From: stormreaver
Written: 2009-02-22 17:04:32.497919
Subject: Printing With Java

This post is to provide a couple simple tips for printing under Java. Before I get into details, though, I want to congratulate SUN for giving Java printing a much needed makeover. Printing with prior versions of Java was unbearably slow and clumsy, to the point of being completely unusable.

That is no longer the case. Modern Java implementations are very fast at printing, and fairly easy to use (once you have a couple good tips to follow). With that out of the way, here are a couple general tips to make printing easy. Keep in mind that this is not a tutorial, but just high-level advice.

Most Java printing tutorials try to keep things simple by assuming you're always going to print a single page, and those tutorials are fine if you are indeed just printing a single page. If you're printing multiple pages, then the techniques used in those tutorials quickly become hopelessly complex.

The Java print engine has a common function that gets called when a page needs to be rendered. Rather than lumping all your print logic into this one function, create a page class that can represent a single printed page and that is capable of rendering that one page. When the user prints the document (whatever that document may be), create an instance of your page class for each printed page, filling it with just the data needed for that page. Do this for all pages *before* you invoke the Java print engine, and store those pages in a List.

Then invoke the Java print engine. When the common print function requests a page, you retrieve the corresponding page object from the List, and invoke your render/print/whatever function that actually lays out the data on the printed page. This is highly efficient because the Java print engine can (and almost always does) request the same page multiple times, and can (and frequently does) request pages in any arbitrary order.

Since you generate the pages only once before invoking the Java print engine, you don't have to waste processing time re-creating the same page over and over again, and you don't have to struggle with figuring out which data belong to which page. This is surprisingly difficult to do if the entire document is lumped together and you don't have any existing reference of what has already been printed.

The other tip I recommend to make your printing much easier than it would otherwise be is to use real-world units of measure when generating items for the printed page. I use a class that represents a single printed item; an item can be a line of text, an image, an entire paragraph, or something else entirely. The item stores its location, width, and height in inches, and knows the page's dots per inch. The page class stores a list of page items, and calculates the physical location of the items at creation time. This technique lends itself to a whole host of features that would be much more complex using more artificial representations such as pixels.

Once you use these simple techniques, the Java printing API becomes extremely flexible and powerful.
You must register an account before you can reply.