Java Reference
In-Depth Information
public PageFormat getPageFormat(int pageIndex) {
// Get the default page format and its Paper object
PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage();
Paper paper = pageFormat.getPaper();
if(pageIndex==0) { // If it's the cover page...
// ...make the margins twice the size
double leftMargin = paper.getImageableX(); // Top left corner
// is indented
double topMargin = paper.getImageableY(); // by the left and
// top margins
// Get right and bottom margins
double rightMargin =
paper.getWidth()-paper.getImageableWidth()-leftMargin;
double bottomMargin =
paper . getHeight()-paper . getImageableHeight()-topMargin;
// Double the margin sizes
leftMargin *= 2.0;
rightMargin *= 2.0;
topMargin *= 2.0;
bottomMargin *= 2.0;
// Set new printable area
paper.setImageableArea(leftMargin, topMargin,
paper.getWidth()-leftMargin-rightMargin,
paper.getHeight()-topMargin-bottomMargin);
// Restore the paper
pageFormat.setPaper(paper);
} else { // We are printing a sketch so decide
// on portrait or landscape
Rectangle rect = theApp.getModel().getModelExtent(); // Get sketch
// bounds
// If the width is more than the height, set landscape
if(rect.width>rect.height) {
pageFormat.setOrientation(pageFormat.LANDSCAPE);
}
}
return pageFormat; // Return the page format
}
Since we are going to deal with the cover page as a special page, we don't really have to adjust the
paper margins here - we could just adjust the position of the text when we print the cover page.
However, this way the page format is determined independently of the print operation, which is a more
sensible approach, and you get to see how margins can be set.
Search WWH ::




Custom Search