[英]Java printing. Prints only one page when using `Book` class
I use Book
class to provide different orientation to pages when printing PDF document. 打印PDF文档时,我使用
Book
类为页面提供不同的方向。
But when I use Book class prints only first page. 但是当我使用Book类时,仅打印第一页。 Other pages doesn't printing.
其他页面无法打印。 But
Book#getNumberOfPages
return me 4
. 但是
Book#getNumberOfPages
返回了我4
。
My code looks like this: 我的代码如下所示:
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;
}
};
}
Please note : I'm use this code to print document: 请注意 :我使用此代码来打印文档:
DocPrintJob job = prn.createPrintJob();
job.print(myDoc, aset);
ie I not use old API: 即我不使用旧的API:
Book bk = new Book();
job.setPageable(bk);
解决方案是在“书”中放置要打印的最大页数,可能是3或10,这并不意味着它将全部打印10张,仅标记书的大小:
book.append(getPrintable(page, needStop), pageFormat, numPages);
This is the test code I put together... 这是我放在一起的测试代码...
It doesn't print PDF's but test's the change in page orientation and page count. 它不打印PDF,而是测试页面方向和页数的变化。
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import static java.awt.print.Printable.NO_SUCH_PAGE;
import static java.awt.print.Printable.PAGE_EXISTS;
import java.awt.print.PrinterException;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
public class TestBook {
public static void main(String[] args) {
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
HashDocAttributeSet docAttSet = new HashDocAttributeSet();
Doc myDoc = getDoc(docAttSet);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);
// this step is necessary because I have several printers configured
PrintService myPrinter = null;
for (int i = 0; i < services.length; i++) {
String svcName = services[i].toString();
System.out.println("service found: " + svcName);
if (svcName.contains("DocuCentre-III C3100")) {
myPrinter = services[i];
System.out.println("my printer found: " + svcName);
break;
}
}
if (myPrinter != null) {
DocPrintJob job = myPrinter.createPrintJob();
try {
job.print(myDoc, aset);
} catch (Exception pe) {
pe.printStackTrace();
}
} else {
System.out.println("no printer services found");
}
}
public static SimpleDoc getDoc(DocAttributeSet dset) {
Book book = new Book();
for (int i = 0; i < 4; i++) {
PageFormat pageFormat = new PageFormat();
if (i % 2 == 0) {
pageFormat.setOrientation(PageFormat.LANDSCAPE);
} else {
pageFormat.setOrientation(PageFormat.PORTRAIT);
}
boolean needStop = false;
if (3 == i) { // 'needStop' = NO_SUCH_PAGE
needStop = true;
}
book.append(getPrintable(i, needStop), pageFormat);
}
System.out.println("Book = " + book.getNumberOfPages());
return new SimpleDoc(book, DocFlavor.SERVICE_FORMATTED.PAGEABLE, dset);
}
private static Printable getPrintable(int page, boolean needStop) {
return new MyPage(page, needStop);
}
public static class MyPage implements Printable {
private int page;
private boolean needStop;
public MyPage(int page, boolean needStop) {
this.page = page;
this.needStop = needStop;
}
public int print(Graphics g, PageFormat pageFormat, int index) throws PrinterException {
g.translate((int)pageFormat.getImageableX(), (int)pageFormat.getImageableY());
System.out.println("Printing " + page);
Font font = new Font("Arial", Font.BOLD, 128);
g.setColor(Color.BLACK);
g.setFont(font);
g.drawString(String.valueOf(index), 0, g.getFontMetrics().getAscent());
// if (needStop) {
// return NO_SUCH_PAGE;
// }
return PAGE_EXISTS;
}
}
}
From the looks of it, your needStop
was misplaced. 从外观
needStop
,您的needStop
放错了位置。 This should only be called when no more pages actually need to be printed, but the way I read your code (and I could be wrong) it seemed to want to skip the last page. 仅当实际上不再需要打印更多页面时才应调用此方法,但是我阅读代码的方式(可能是错误的)似乎要跳过最后一页。
However, after testing, it would seem you don't actually need it (as the Book
knows how many pages it wants to print ;)) 但是,经过测试后,您似乎实际上并不需要它(因为
Book
知道它要打印多少页;)
You should implement print()
method of Printable
like this: 您应该像这样实现
Printable
print()
方法:
@Override
public int print(Graphics g, PageFormat pf, int pageNumber)
throws PrinterException {
if (pageNumber < book.getNumberOfPages()) {
// Printing code here
return PAGE_EXISTS;
}
return NO_SUCH_PAGE;
}
For this, you must have reference to your book
instance which can be achieved by making a inner class Page
implementing Printable
interface with the above print()
method definition. 为此,您必须参考您的
book
实例,可以通过使用上面的print()
方法定义使内部类Page
实现Printable
接口来实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.