Java Reference
In-Depth Information
A class that implements the
Pageable
interface defines an object that represents a set of pages to be printed
rather than a single page. You would implement this interface for more complicated printing situations where
a different page painter may print each page using an individual
PageFormat
object. It's the job of the
Pageable
object to supply information to the
PrinterJob
object about which page painter and
PageFormat
object should be used to print each page. The
Pageable
interface declares three methods:
getNumberOfPages()
Returns the number of pages to be printed as
type
int
, or the constant value
Pageable.UNKNOWN
_
NUMBER
_
OF
_
PAGES
if
the number of pages is not known.
getPageFormat(int pageIndex)
Returns the
PageFormat
object describing
the size and orientation of the page specified
by the argument. An exception of type
IndexOutOfBoundsException
will be
thrown if the page does not exist.
getPrintable(int pageIndex)
Returns a reference to the
Printable
object
responsible for printing the page specified by
the argument. An exception of type
IndexOutOfBoundsException
will be
thrown if the page does not exist.
A
Book
object also encapsulates a document that consists of a number of pages, each of which may be
processed individually for printing. The difference between this and an object of a class that implements
Pageable
is that you can add individual pages to a
Book
object programmatically, whereas a class
implementing
Pageable
encapsulates all the pages. We will look at how both of these options work
later in this chapter.
Creating and Using PrinterJob Objects
Because the
PrinterJob
class encapsulates and manages the printing process for a given printer that is
external to the JVM, you can't create an object of type
PrinterJob
directly using a constructor. You
can obtain a reference to a
PrinterJob
object for the default printer on a system by calling the static
method
getPrinterJob()
that is defined in the
PrinterJob
class:
PrinterJob printJob = PrinterJob.getPrinterJob(); // For the default printer
The object
printJob
provides the interface to the default printer, and controls each print job that you
send to it.
A printer is encapsulated by a
PrintService
object, and you can obtain a reference of type
PrintService
to the object encapsulating the printer that will be used by a
PrinterJob
object by
calling its
getPrintService()
method:
PrintService printer = printJob().getPrintService();