Java打印。使用“Book”类时只打印一页 [英] Java printing. Prints only one page when using `Book` class

查看:439
本文介绍了Java打印。使用“Book”类时只打印一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 预订 类可在打印PDF文档时为页面提供不同的方向。

I use Book class to provide different orientation to pages when printing PDF document.

但是当我使用Book类时只打印第一页。其他页面不打印。但预订#getNumberOfPages 给我 4

But when I use Book class prints only first page. Other pages doesn't printing. But Book#getNumberOfPages return me 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;

                }
            };
    }

请注意:我使用此代码打印文档:

Please note: I'm use this code to print document:

DocPrintJob job = prn.createPrintJob();
job.print(myDoc, aset);

即。我没有使用旧的API:

i.e. I not use old API:

Book bk = new Book();       
job.setPageable(bk);    


推荐答案

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

The solution is to place the maximum number of pages to print in the "book", maybe 3 or 10 ,that does not mean that it will print all that 10, only mark the size of the book:

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

这篇关于Java打印。使用“Book”类时只打印一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆