Java打印。使用“Book”类时只打印一页

时间:2013-04-16 05:47:17

标签: java printing

我在打印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);    

3 个答案:

答案 0 :(得分:1)

解决方案是在“书”中放置要打印的最大页数,可能是3或10,这并不意味着它将打印所有10,只标记书的大小:

book.append(getPrintable(page, needStop), pageFormat, numPages);

答案 1 :(得分:0)

这是我放在一起的测试代码......

它不会打印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;

        }
    }
}

从它的外观来看,你的needStop放错了地方。这应该仅在不再需要打印页面时调用,但是我读取代码的方式(我可能错了)似乎想跳过最后一页。

然而,在测试之后,似乎你实际上并不需要它(因为Book知道它想要打印多少页;)

答案 2 :(得分:0)

您应该像print()这样实施Printable方法:

@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;
}

为此,您必须引用您的book实例,这可以通过使用上述Page方法定义实现Printable内部print()接口来实现。