try {
origPage = document.getPageFormat(pageIndex);
page = (PageFormat)origPage.clone();
painter = document.getPrintable(pageIndex);
} catch (Exception e) {
PrinterException pe =
new PrinterException("Error getting page or printable.[ " +
e +" ]");
pe.initCause(e);
throw pe;
}
/* Get the imageable area from Paper instead of PageFormat
* because we do not want it adjusted by the page orientation.
*/
Paper paper = page.getPaper();
// if non-portrait and 270 degree landscape rotation
if (page.getOrientation() != PageFormat.PORTRAIT &&
landscapeRotates270) {
double left = paper.getImageableX();
double top = paper.getImageableY();
double width = paper.getImageableWidth();
double height = paper.getImageableHeight();
paper.setImageableArea(paper.getWidth()-left-width,
paper.getHeight()-top-height,
width, height);
page.setPaper(paper);
if (page.getOrientation() == PageFormat.LANDSCAPE) {
page.setOrientation(PageFormat.REVERSE_LANDSCAPE);
} else {
page.setOrientation(PageFormat.LANDSCAPE);
}
}
double xScale = getXRes() / 72.0;
double yScale = getYRes() / 72.0;
/* The deviceArea is the imageable area in the printer's
* resolution.
*/
Rectangle2D deviceArea =
new Rectangle2D.Double(paper.getImageableX() * xScale,
paper.getImageableY() * yScale,
paper.getImageableWidth() * xScale,
paper.getImageableHeight() * yScale);
/* Build and hold on to a uniform transform so that
* we can get back to device space at the beginning
* of each band.
*/
AffineTransform uniformTransform = new AffineTransform();
/* The scale transform is used to switch from the
* device space to the user's 72 dpi space.
*/
AffineTransform scaleTransform = new AffineTransform();
scaleTransform.scale(xScale, yScale);
/* bandwidth is multiple of 4 as the data is used in a win32 DIB and
* some drivers behave badly if scanlines aren't multiples of 4 bytes.
*/
int bandWidth = (int) deviceArea.getWidth();
if (bandWidth % 4 != 0) {
bandWidth += (4 - (bandWidth % 4));
}
if (bandWidth <= 0) {
throw new PrinterException("Paper's imageable width is too small.");
}
int deviceAreaHeight = (int)deviceArea.getHeight();
if (deviceAreaHeight <= 0) {
throw new PrinterException("Paper's imageable height is too small.");
}
/* Figure out the number of lines that will fit into
* our maximum band size. The hard coded 3 reflects the
* fact that we can only create 24 bit per pixel 3 byte BGR