Chapter 21. The java.awt.print PackageThe java.awt.print package contains classes and interfaces that support printing. It has been introduced in Java 1.2 and supersedes the PrintJob class of the java.awt package. The most important class in this package is PrinterJob; it coordinates the printing process. The Printable and Pageable interfaces represent printable objects or documents that can be printed with a PrinterJob. Figure 21-1 shows the class hierarchy of this package. See Chapter 5, "Printing", for more details on printing. Figure 21-1. The java.awt.print package
This class implements a multi-page Pageable document out of individual single-page Printable objects. Each page of the Book can have its own PageFormat specified. You might use this class, for example, in a database-reporting application when you want to print a report that consists of single-page tables and graphs in mixed portrait and landscape modes. The append() and setPage() methods allow you to specify the Printable pages that make up the book, along with their PageFormat objects. The remaining methods implement the Pageable interface and are used by a PrinterJob to print the Book. As with all Pageable objects, the Printable objects added to a Book must print only a single page. (See Pageable for details.) Note that the three-argument version of append() takes a numPages argument. This argument does not specify the number of pages in the Printable; instead, it specifies the number of identical copies of the single-page Printable to be printed.
Hierarchy: Object-->Book(Pageable)
This interface is implemented by multipage documents that know how to print themselves in more sophisticated ways than the Printable interface allows. There are two primary differences between Pageable and Printable. The first is that a Pageable object knows how many pages are in the document and can print them in an arbitrary order. This allows a printer dialog to give the user the choice to print only a range of pages within the document. It also allows the user to request that the pages be printed in reverse order, which is useful on some printers that stack document pages in reverse order. The second difference between Pageable and Printable is that each page of a Pageable may have a different PageFormat associated with it. This is useful when a document contains pages in portrait orientation and some pages in landscape orientation, for example. The heart of the Pageable interface is the method getPrintable(), which returns a Printable object able to print the specified page of the document. The Printable objects returned by this method are used differently and must be implemented differently than Printable objects that are designed to be printed alone. While standalone Printable objects may print multiple pages, the Printable objects returned by a Pageable represent only a single page. In other words, the getPrintable() method of Pageable is called once for every page in the document, and the Printable it returns is used to print only that one page. The Pageable interface lends itself to two distinct implementation strategies. In the first, the getPrintable() method returns a different Printable object for each page of the document. This Printable object knows exactly what page to print and can ignore the pageIndex argument passed to its print() method. The second strategy is to implement a single Printable object and have the getPrintable() method always return this one object, regardless of page number. In this case, the print() method uses the pageIndex argument to decide what page to print. Note that a Printable implemented in this way must be more flexible than a standalone Printable object, since its pages may be printed in any order and are not guaranteed to be accessed sequentially. Finally, as with standalone Printable objects, Printable objects returned by Pageable objects must be prepared to have their print() methods called multiple times for the same page number. This is a requirement for banded raster printing in low-memory environments. See Printable for details.
Implementations: Book Passed To: PrinterJob.setPageable()
This class is a combination of a Paper object that specifies page size and imageable area with a field that specifies the orientation of the page. The orientation values are the following:
Printable and Pageable objects are not responsible for performing the rotations necessary to print in these orientations. The appropriate transform has already been done in the Graphics object passed to the print() method of Printable.
Hierarchy: Object-->PageFormat(Cloneable) Passed To: Book.{append(), setPage()}, Printable.print(), PrinterJob.{defaultPage(), pageDialog(), setPrintable(), validatePage()} Returned By: Book.getPageFormat(), Pageable.getPageFormat(), PrinterJob.{defaultPage(), pageDialog(), validatePage()}
This class describes the width, height, and imageable area of a piece of paper. The imageable area is the region of a page that should be printed on--the area inside the margins. Most printers cannot print all the way to the edges of the page and require margins of at least a quarter inch on all sides. All coordinates and sizes used by this class are measured in printer's points, where one point is defined as exactly 1/72nd of an inch.
Hierarchy: Object-->Paper(Cloneable) Passed To: PageFormat.setPaper() Returned By: PageFormat.getPaper()
This interface is implemented by objects that know how to print themselves. It should be implemented by objects that always print a single page, or by multipage documents that know how to print their pages in sequential order only. Multipage documents that are able to print their pages in arbitrary order should implement the somewhat more complex Pageable interface. When a PrinterJob prints a Printable object, it calls the print() method one or more times to print the pages. This method should print the page specified by pageIndex, using the specified Graphics object and the page size and orientation specified in the PageFormat object. The PrinterJob guarantees that it prints the pages of a Printable in strictly sequential order. The pageIndex argument begins at 0 and increases. After printing a page, the print() method should return the PAGE_EXISTS constant. Since the PrinterJob has no way of knowing how many pages there are in a Printable object, it keeps increasing the page number until the print() method returns the NO_SUCH_PAGE constant. When this value is returned, the PrinterJob knows that it has reached the end of the print job. There is a very important twist in this communication protocol between PrinterJob and Printable. While the PrinterJob guarantees that it does not try to print pages out of order, it is allowed to print the same page multiple times. This means, for example, that the print() method may be called with a pageIndex argument of 0 three times in a row. Printable objects must be implemented with this possibility in mind. The reason that this is necessary is that, in some cases, printing is done into a very large (high-resolution) off-screen image, and this image data is then transferred to the printer. On systems with limited memory, this printing technique must be done in multiple passes, printing only a fraction, or band, of the page at each pass. The Printable interface is also used by Pageable objects that know how to print their pages out of order. When a Printable object is returned by a Pageable object, the PrinterJob uses it differently than it does a standalone Printable object. See Pageable for details.
Passed To: Book.{append(), setPage()}, PrinterJob.setPrintable() Returned By: Book.getPrintable(), Pageable.getPrintable()
Signals that a print job has been aborted, typically because of a user request to terminate it.
Hierarchy: Object-->Throwable(Serializable)-->Exception-->PrinterException-->PrinterAbortException
This class serves as the superclass for all exceptions that may arise in the process of printing. See the subclasses PrinterAbortException and PrinterIOException. PrinterException and its subclasses are checked exceptions, they must be declared in the throws clauses of methods that may throw them.
Hierarchy: Object-->Throwable(Serializable)-->Exception-->PrinterException Subclasses: PrinterAbortException, PrinterIOException Thrown By: Printable.print(), PrinterJob.print()
This interface is implemented by any Graphics object that is passed to the print() method of a Printable object. This means that a Printable can always cast its Graphics object to a PrinterGraphics object and use the getPrinterJob() method to obtain the PrinterJob that is in use.
Indicates that an I/O error occurred during the printing process. This usually means that the PrinterJob had trouble communicating with the printer or print server.
Hierarchy: Object-->Throwable(Serializable)-->Exception-->PrinterException-->PrinterIOException
This class controls printing of Printable and Pageable objects. Applications typically use this class as follows:
PrinterJob is the preferred class for printing in Java 1.2. Do not confuse it with java.awt.PrintJob, which was introduced in Java 1.1.
Returned By: PrinterGraphics.getPrinterJob(), PrinterJob.getPrinterJob() Copyright © 2001 O'Reilly & Associates. All rights reserved. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|