我刚开始to learn how to print a window in Java/Swing。 (编辑:刚找到the Java Printing Guide)
当我这样做时:
protected void doPrint() {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
boolean ok = job.printDialog();
if (ok) {
try {
job.print();
}
catch (PrinterException ex) {
ex.printStackTrace();
}
finally {
}
}
}
我得到了这个打印机对话框(在Windows XP上):
如何更改页面范围,使其不是1-9999?
编辑:使用Pageable
/ Book
设置页面范围(正如@t_barbz帮助指出的那样)需要PageFormat,在这种情况下我有一个catch-22,因为我我喜欢打印对话框来选择它,我似乎没有从打印对话框中获得返回值。
答案 0 :(得分:4)
对于页面范围,我认为您需要使用PrinterJob的setPageable(Pageable文档)方法。看起来应该这样做。
protected void doPrint() {
PrinterJob job = PrinterJob.getPrinterJob();
Book book = new Book();
book.append(this, job.defaultPage());
printJob.setPageable(book);
boolean ok = job.printDialog();
if (ok) {
try {
job.print();
}
catch (PrinterException ex) {
ex.printStackTrace();
}
finally {
}
}
}
答案 1 :(得分:1)
最后这是一个简单的代码:
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
PrintRequestAttributeSet printAttribute = new HashPrintRequestAttributeSet();
printAttribute.add(new PageRanges(1, 100));
boolean ok = job.printDialog(printAttribute);
if (ok) {
try {
job.print();
} catch (PrinterException ex) {
/* The job did not successfully complete */
}
}