在打印 PDF 文档时,我使用Book
类为页面提供不同的方向。
但是当我使用 Book 类时,只打印第一页。其他页面不打印。但是 Book#getNumberOfPages
还给我4
。
我的代码如下所示:
public static getDoc(DocAttributeSet dset) {
final PDFFile pdfFile = new PDFFile(buf);
Book book = new Book();
for (int i=0; i<pdfFile.getNumPages(); i++) {
PDFPage page = pdfFile.getPage(i);
PageFormat pageFormat = new PageFormat();
if (page.getAspectRatio() >= 1) {
pageFormat.setOrientation(PageFormat.LANDSCAPE);
} else {
pageFormat.setOrientation(PageFormat.PORTRAIT);
}
boolean needStop = false;
if (pdfFile.getNumPages() - 1 == i ) { // if latest page, then stopping ('needStop' = NO_SUCH_PAGE)
needStop = true;
}
book.append(getPrintable(page, needStop), pageFormat);
}
return new SimpleDoc(book, DocFlavor.SERVICE_FORMATTED.PAGEABLE, dset);
}
private static Printable getPrintable(final PDFPage page, final boolean needStop) {
return new Printable() {
public int print(Graphics g, PageFormat pageFormat, int index) throws PrinterException {
if (needStop) {
return NO_SUCH_PAGE;
}
// no scaling, center PDF
... // code omitted
return PAGE_EXISTS;
}
};
}
请注意:我使用此代码打印文档:
DocPrintJob job = prn.createPrintJob();
job.print(myDoc, aset);
即我不使用旧 API:
Book bk = new Book();
job.setPageable(bk);