Java Reference
In-Depth Information
Printing Using a Book
A Book object is a repository for a collection of pages where the pages may be printed using different
formats. The page painter for a page in a book is represented by a Printable object and each
Printable object within a book can print one or possibly several pages with a given format.
Book Object
Printable Object
Printable object
Printable object
Page 1
Page 5
Page 6
Page 0
Page 2
Page 3
Page 4
All pages produced by a given
object in a book will use
the same
Printable
PageFormat
Object
Because the Book class implements the Pageable interface, you print a book in the same way as you print a
Pageable object. Once you have assembled all the pages you want in a Book object, you just pass a
reference to it to the setPageable() method for your PrinterJob object. Let's take it from the top.
The Book class only has a default constructor, and that creates an empty book. Thus, we create a
book like this:
Book sketchBook = new Book();
You add a page to a book by calling the append() method for the Book object. There are two
overloaded versions of append() : one to add a Printable object that represents a single page, and
the other to add a Printable object that represents several pages. In the latter case, all the pages are
printed using the same PageFormat object.
The first version of append() accepts two arguments: a reference to the Printable object and a
reference to an associated PageFormat object. We could add the cover page of a sketch just as in the
previous example to the sketchBook object like this:
PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage();
Paper paper = pageFormat.getPaper();
double leftMargin = paper.getImageableX(); // Top left corner is indented
double topMargin = paper.getImageableY(); // by the left and top margins
double rightMargin = paper.getWidth()-paper.getImageableWidth()-leftMargin;
double bottomMargin = paper.getHeight()-paper.getImageableHeight()-topMargin;
leftMargin *= 2.0; // Double the left margin...
Search WWH ::




Custom Search