Java Reference
In-Depth Information
Paper paper = pageFormat.getPaper();
final double MM_TO_PAPER_UNITS = 72.0/25.4; // 25.4 mm to an inch
final double widthA4 = 210*MM_TO_PAPER_UNITS;
final double heightA4 = 297*MM_TO_PAPER_UNITS;
paper.setSize(widthA4, heightA4);
If you use letter size paper that is 8.5 by 11 inches, it's somewhat simpler:
Paper paper = pageFormat.getPaper();
final double widthLetterSize = 72.0*8.5;
final double heightLetterSize = 72.0*11.0;
paper.setSize(widthLetterSize, heightLetterSize);
The setImageableArea() method expects you to supply four arguments of type double . The first two
are the coordinates of the top-left corner of the printable area and the next two are the width and the height.
All these values are in units of 1/72 of an inch. To set 20 mm margins on your A4 sheet you could write the
following:
double marginSize = 20.0* MM_TO_PAPER_UNITS; // 20 mm wide
paper.setImageableArea(marginSize, marginSize, // Top left
widthA4-2.0*marginSize, // Width
heightA4-2.0*marginSize); // Height
If you are printing on letter-size paper, a one-inch margin might be more appropriate, so you would write
this:
double marginSize = 72.0; // 1 inch wide
paper.setImageableArea(marginSize, marginSize, // Top left
widthLetterSize-2.0*marginSize, // Width
heightLetterSize-2.0*marginSize); // Height
Of course, there's no reason why a class that implements the Pageable interface cannot also implement
Printable , so you could do this in Sketcher just to get a feel for the Pageable interface in action.
TRY IT OUT: Using the Pageable Interface
You just print two pages in a print job in Sketcher, a cover page with a title for the sketch plus the sketch
itself, which may be portrait or landscape, of course. You could produce both pages in SketcherView ,
but to make it more interesting, let's define a separate class to represent a Printable object for the cover
page:
import java.awt.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.awt.font.TextLayout;
Search WWH ::




Custom Search