java 打印时设置景观方向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12192656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting LANDSCAPE orientation when printing
提问by MyTitle
I need to print an image. When I set orientation like
我需要打印图像。当我设置方向时
printRequestAttributeSet.add(OrientationRequested.LANDSCAPE);
printRequestAttributeSet.add(OrientationRequested.LANDSCAPE);
all works fine.
一切正常。
But when I set orientation inside print()
method of Printable
:
但是当我在print()
方法中设置方向时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.
它不适用于第一页。即它以横向模式打印除第一页之外的所有页面。
回答by MadProgrammer
You can not change the orientation when your already trying to print the page.
当您已经尝试打印页面时,您无法更改方向。
If you need to provide a number of pages with different orientations, you will want to look at the Bookand Pageableinterfaces, see Printing in Javafor examples.
如果您需要提供具有不同方向的多个页面,您将需要查看Book和Pageable接口,请参阅用 Java 打印以获取示例。
The only other solution you have is to rotate the image in Printable
, which is troublesome at best.
您唯一的其他解决方案是在 中旋转图像Printable
,这充其量只是麻烦。
ps - Printing is fun...when it works ;)
ps - 打印很有趣……什么时候能用 ;)
回答by Kalpesh A. Nikam
Place pf.setOrientation(PageFormat.LANDSCAPE)
before calling printJob
pf.setOrientation(PageFormat.LANDSCAPE)
打电话前的地方printJob
ex.
前任。
if(pay_type.getSelectedItem().equals("EMI"))
{
EMI();
}
else{
validation();
}
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable((Printable) this);
boolean ok = job.printDialog();
if (ok) {
try {
//here set page oriatetion
job.print();
} catch (PrinterException ex) {
/* The job did not successfully complete */
}
}