我需要打印一张图片。当我像
这样设置方向时printRequestAttributeSet.add(OrientationRequested.LANDSCAPE);
一切正常。
但是当我在 Printable
的 print()
方法中设置方向时:
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;
};
它不适用于第一页。即它以横向模式打印除第一页以外的所有页面。
最佳答案
当您已经尝试打印页面时,您不能更改方向。
如果您需要提供多个不同方向的页面,您需要查看 Book和 Pageable接口(interface),参见 Printing in Java例如。
唯一的其他解决方案是在 Printable
中旋转图像,这充其量也很麻烦。
ps - 打印很有趣......当它工作时;)
关于java - 打印时设置横向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12192656/