java中如何设置打印页面的大小?
我编写了一个程序,它使用 Java 打印 API 从打印机打印页面。我相信我已经输入代码将页面大小设置为字母,但它仍然以打印机默认的任何大小打印。这是我的 printPage()
方法:
public void printPage() {
getTot();
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pf = job.defaultPage();
Paper paper = pf.getPaper();
paper.setSize(8.5 * 72, 11 * 72);
paper.setImageableArea(0.5 * 72, 0.0 * 72, 7.5 * 72, 10.5 * 72);
pf.setPaper(paper);
job.setPrintable(this);
boolean ok = job.printDialog();
if (ok) {
if (cov)
try {
for (j = 0; j < printPaths.size(); j++)
job.print();
cov = false;
} catch (PrinterException ex) {
System.out.println(ex.getMessage());
}
if (summ)
try {
job.print();
summ = false;
} catch (PrinterException ex) {
System.out.println(ex.getMessage());
}
}
}
我做错了什么?
I have written a program that uses the Java print API to print pages from a printer. I believe I have put in code to set the page size to letter, but it still prints on whatever size is default for the printer. Here is my printPage()
method:
public void printPage() {
getTot();
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pf = job.defaultPage();
Paper paper = pf.getPaper();
paper.setSize(8.5 * 72, 11 * 72);
paper.setImageableArea(0.5 * 72, 0.0 * 72, 7.5 * 72, 10.5 * 72);
pf.setPaper(paper);
job.setPrintable(this);
boolean ok = job.printDialog();
if (ok) {
if (cov)
try {
for (j = 0; j < printPaths.size(); j++)
job.print();
cov = false;
} catch (PrinterException ex) {
System.out.println(ex.getMessage());
}
if (summ)
try {
job.print();
summ = false;
} catch (PrinterException ex) {
System.out.println(ex.getMessage());
}
}
}
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试添加这段代码并重新运行:
而不是
Can you try adding this piece of code and rerun :
instead of
本书对我的多页示例不起作用(结果是仅打印第一页),但它给了我另一个想法。而不是:
我尝试了另一种重载方法,它适用于所有页面:
如果您想要自定义纸张尺寸,则需要将其作为参数转发给 print 方法(Java 会为您执行此操作,但您必须将其传递给 setPrintable 方法)。方法开始执行后更改 PaperFormat 仅对第二页及以后的页面有影响,或者根本没有任何影响。
不管怎样,谢谢你们俩。
Book didn't work for me with my multiple pages example (the result was the printing of only the first page), but it gave me another idea. Instead of :
I tried another overloaded method and it worked for all pages:
If you want custom paper size, you need to forward it to print method as an argument (which Java will do for you but you must pass it to setPrintable method). Changing PaperFormat after method starts to execute will have an effect only on the second and further pages or not have any effect at all.
Anyway, thanks to you both.