gpt4 book ai didi

java - 首页上的页面打印帮助

转载 作者:太空宇宙 更新时间:2023-11-04 08:15:31 26 4
gpt4 key购买 nike

我有一个名为 myPrintableObjectPrintable 类,并且 print 方法通过以下方式重写:

public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException
{
if(pageIndex<5)
{
pf.setOrientation(PageFormat.LANDSCAPE);
g.drawString("HELLO FRIEND",100,180);
return PAGE_EXISTS;
}
else
{return NO_SUCH_PAGE;}

}

我想在同一个文档中横向打印多页。除第一页外正在打印。它总是以纵向打印。

我该如何解决这个问题?

最佳答案

你在这里:

    PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pf = job.defaultPage();
pf.setOrientation(PageFormat.LANDSCAPE);
job.setPrintable(myPrintableObject, pf);

工作示例:

public class MyPrintable implements Printable {

public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex < 5) {
graphics.drawString("HELLO FRIEND", 100, 180);
return PAGE_EXISTS;
} else {
return NO_SUCH_PAGE;
}
}

public static void main(String[] args) {
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pf = job.defaultPage();
pf.setOrientation(PageFormat.LANDSCAPE);
job.setPrintable(new MyPrintable(), pf);

boolean ok = job.printDialog();
if (ok) {
try {
job.print();
} catch (PrinterException ex) {
/* The job did not successfully complete */
}
}
}
}

关于java - 首页上的页面打印帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10495800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com