I need to print an image. When I set orientation like
printRequestAttributeSet.add(OrientationRequested.LANDSCAPE);
all works fine.
But when I set orientation inside print()
method of Printable
:
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex >= images.size())
return Printable.NO_SUCH_PAGE;
image = images.get(pageIndex);
// if image width>height --> Landscape, else --> Protrait
if (image.getWidth(null) > image.getHeight(null))
pageFormat.setOrientation(PageFormat.LANDSCAPE);
else
pageFormat.setOrientation(PageFormat.PORTRAIT);
graphics2D = (Graphics2D) graphics;
graphics.drawImage(image, 0, 0, image.getWidth(null), image.getHeight(null), null);
return PAGE_EXISTS;
};
it doesn't work with first page. i.e. it prints all pages in Landscape mode except 1-st page.