Java matured very quickly in most respects after it was first introduced, but for a long time, printing was one of Java's
weakest points. In fact, Java 1.0 didn't offer any support for printing at all. Java 1.1 included a class called PrintJob in the java.awt package, but the printing capabilities supported by that class were somewhat crude and unreliable. When Java 1.2 (or "Java 2") was
introduced, it included a completely separate mechanism (called the Java 2D printing API) for printing designed around PrinterJob and other classes and interfaces defined in the new java.awt.print package. This rendered the PrintJob-based printing mechanism (also known as AWT printing) largely obsolete, although PrintJob has never been deprecated and, at least of this writing, is still technically a supported class.
Additional changes were made in J2SE 1.3 when PrintJob's capabilities expanded to allow the setting of job and page attributes using the appropriately named JobAttributes and PageAttributes classes within the java.awt package. With the release of J2SE 1.3, the printing capabilities were reasonably robust, but some problems still existed
aside from the confusion associated with having two completely separate printing facilities. For one thing, both facilities
used an implementation of the java.awt.Graphics class for rendering the content to be printed, which meant anything that needed to be printed had to be rendered as a graphical
image. In addition, the newer and generally more robust PrinterJob facility provided only limited support for setting attributes associated with the job. Finally, neither facility provided
a way to programmatically select the target printer.
The biggest change in Java's printing capabilities to date came with the release of J2SE 1.4, when the Java print service
API was introduced. This third implementation of printing support in Java addressed the limitations that were just described
using an implementation of the PrintService and DocPrintJob interfaces defined in the javax.print package. Because this new API represents a superset of the functionality defined by the two older printing facilities, it's
the one you should normally use and will be the focus of this article.
At a high level, the steps involved in using the Java print service API are straightforward:
PrintService implementations.
createPrintJob() method defined in the PrintService interface. The print job is represented by an instance of DocPrintJob.
Doc interface that describes the data you want to print. You also have the option of creating an instance of PrintRequestAttributeSet that describes the printing options you want.
print() method defined in the DocPrintJob interface, specifying the Doc you created in the previous step and the PrintRequestAttributeSet or a null value.
You'll now examine each of these steps and see how to achieve them.
| Subject |
|
|
|
|
|
|
It ia good article!!!By Anonymous on September 27, 2009, 11:22 amwo like it very much!!!!
Reply | Read entire comment
Very nice explanationBy Anonymous on August 12, 2009, 3:03 amI read many articles/tutorials about Java printing. This one is the best. Thank you.
Reply | Read entire comment
Discover Printer by NameBy Anonymous on February 19, 2009, 6:22 pmExcellent article! What are the rules for the name that is returned by PrintService.getName()? It seems that the name always looks like \\Server\PrinterName.
Reply | Read entire comment
excellent article!By Anonymous on October 3, 2008, 7:07 pmI was looking for help on a troubleshooting job and don't know Java at all. This article gave me perspective that made a big difference, and technical knowledge...
Reply | Read entire comment
View all comments