Java Reference
In-Depth Information
At the start of a print job that was initiated by a call to the setPageable() method for a PrinterJob
object, the PrinterJob object calls the getNumberOfPages() method for the Pageable object to determine
how many pages are to be printed. If you return the value UNKNOWN_NUMBER_OF_PAGES, then the process
relies on a Printable object returning NO_SUCH_PAGE at some point to stop printing. It is therefore a good
idea to supply the number of pages when it can be determined.
The PrinterJob object assumes each page in a print job is associated with a page index value, with page
index values starting at 0. For each page index, the PrinterJob object calls the getPageFormat() meth-
od to obtain the PageFormat object to be used to print the page, and then calls getPrintable() for the
Pageable object to obtain a reference to the Printable object that does the printing. Of course, just be-
cause you can supply a different Printable object for each page doesn't mean that you must . You can use
as many or as few as you need for your application, and control how different pages are printed by mak-
ing the getPageFormat() method for the Pageable object return different PageFormat objects. Remember,
though, that the print() method for a Printable object may be called more than once by the PrinterJob
object to print a particular page, and the same page should be rendered each time the same page index is
passed as an argument to the print() method, so you must not code the method in a way that presumes
otherwise.
Creating PageFormat Objects
As you saw earlier, you can get the default PageFormat object for the print service you are using by calling
the defaultPage() method for the PrinterJob object. You could use the default PageFormat class con-
structor to create an object that is portrait-oriented, but in this case you have no guarantee that the object is
compatible with the current print service. A PageFormat object encapsulates information about the size of
the paper and the margins in effect, so the object produced by the default constructor may not correspond
with your printer setup. If you want to go this route, you can pass a reference to a PageFormat object to the
validatePage() method for a PrinterJob object. For example:
// Object for current printer
PrinterJob printJob = PrinterJob.getPrinterJob();
// Validated page
PageFormat pageFormat = printJob.validatePage(new PageFormat());
Note that the validatePage() method does not return the reference that you pass as the argument. The
method clones the object that was passed to it and returns a reference to the clone, which has been modified
where necessary to suit the current printer. Because it does not modify the object in place, you always need
to store the reference that is returned. This is obviously well suited to multipage printing because you can
create a series of distinct PageFormat objects from the same argument.
Fundamentally, a PageFormat object encapsulates all the information needed to print on a page, as Figure
21-14 illustrates.
FIGURE 21-14
 
 
Search WWH ::




Custom Search