Skip to main content

Printing with AWT, believe it or not

Posted by larryjava on July 23, 2012 at 8:00 AM PDT

It never ceases to amaze me where some solutions in application development come from.

We had a requirement for our application to allow a user to print pages of text.
The user would bring up a web page, from our app, and then create & print a list of customer names.
The list of names would be created by executing a SQL query against the database, using a variety of search criteria.
The users wanted the option to preview the pages to be printed and
there were specific Font sizes that had to be used for the text.
The number of names on the list could vary from a dozen to hundreds. So multiple page printing was required as well.

The first question we asked was, "How do you print with Java?"
Looking at the various Java APIs we found the Javax.print package.

The documentation for Javax.print stated :
Provides the principal classes and interfaces for the Java Print Service API.
1 - Discover and select print services based on their capabilities
2 - Specify the format of print data
3 - Submit print jobs to services that support the document type to be printed.

Upon closer examination of Javax.print, we concluded that items 1 and 3 would give us part of what we wanted.
But item 2 did not include how to put the data on an actual printed page.
So we wondered, "How can we do the actual placing of data on a page to be printed?"

Looking further at the Java API, we found the Java.awt package.

The documentation for Java.awt stated :
Contains all of the classes for creating user interfaces and for painting graphics and images.

At first we thought, "This package is for building GUI apps. It has nothing to do with printing."

We looked at the rest of the Java API and could find nothing to support printing.
We thought we were at a dead-end.

Then we took a second look at Java.awt.
In the documentation, the phrase; "... for painting graphics and images" gave us a clue to our printing mystery.
Could this be what we were looking for? The short answer was --- yes!

In particular, the Java.awt.print package contained the print features we wanted.
We could "render" our text on the print page as if it was a GUI screen.
Mystery solved.

We created a helper\utility class that contained our print methods.
This helper class would be used by the servlet for the web page that controlled this feature in our app.
The helper class implemented the Printable Interface.
It contained the following methods:
- print(); which used setFont(), setColor(), drawString() methods to render the data on the page
- print_pages(); which set print attributes, created the document object, created the print job, and executed actual print command
- create_preview_page_image(); which created a previewable image file of the page to be printed

This part of our application also had to create PDF files, perform text searching and send emails with attachments.
So we had to build a lot of complex features into this part of the app.
In the end, it all worked great. The users got what they wanted. They were able to print their name lists exactly the way they wanted.

We are still amazed that we created a print capability for our application with AWT.
Was this solution the correct way to use AWT?
Was this solution the correct way to do Java printing in an application server environment?
In our case, the answer was yes.
This effort taught us how to create a "print text anywhere" capability with core\pure Java.
We also gained more confidence in solving Java-based development problems on our own.
Anytime we can gain wisdom about using Java, we will take it!