Book
PDFドキュメントを印刷するときに、クラスを使用してページにさまざまな向きを提供します。
しかし、 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);