I need to print images in Java. So I implemented print() method of Printable interface. But printer always prints only some piece of original image. How I can cause image to fit printing area (A4), so printer be able to print whole image, not piece of them? Now my code looks like this:
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex >= images.size()) {
return Printable.NO_SUCH_PAGE;
}
RenderedImage image;
Graphics2D graphics2D = (Graphics2D) graphics;
image = new NullOpImage((RenderedImage) images.get(pageIndex), null, null, OpImage.OP_IO_BOUND);
double x = (pageFormat.getImageableWidth() - image.getWidth())/2 + pageFormat.getImageableX();
double y = (pageFormat.getImageableHeight() - image.getHeight())/2 + pageFormat.getImageableY();
graphics2D.drawRenderedImage(image, AffineTransform.getTranslateInstance(x, y));
return PAGE_EXISTS;
}
You need to scale the image down to fit the available area.