5

出于多种原因,我试图将多个 JTable 的输出组合到一个打印作业中。在尝试构建 PDF 并梳理 Java API 之后,我决定使用 Book 类。我的打印代码目前看起来像这样。

try {       
    PrinterJob printer = PrinterJob.getPrinterJob();

    //Set 1/2 " margins and orientation
    PageFormat pf = printer.defaultPage();
    pf.setOrientation(PageFormat.LANDSCAPE);
    Paper paper = new Paper();
    double margin = 36; // half inch
    paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2);
    pf.setPaper(paper);

    Book printJob = new Book();

    // Note for next line: getAllTables() returns an ArrayList of JTables
    for (JTable t : getAllTables() )  
        printJob.append(t.getPrintable(PrintMode.FIT_WIDTH, null, null), pf,2);

    printer.setPageable(printJob);

    System.out.println(printJob.getNumberOfPages());

    if (printer.printDialog())
        printer.print();
    } catch (PrinterException e) {
        e.printStackTrace();
}

主要问题:只有“第一个”表的输出正在打印。我已确保 for 循环正确迭代,并且调试语句显示每个表都已添加到 Book 中。更改表的顺序没有效果。将打印模式更改为PrintMode.NORMAL, 确实会导致打印其他表格的片段。但是,我遇到了一个水平分页问题,​​因为表格宽度经常超过页面宽度(它仍然不能解释为什么PrintMode.FIT_WIDTH不起作用)

第二个问题:如何检测每个可打印的正确页数?我的大多数表格都是两页长,所以目前,我每次追加时只添加 2 页。我读到“某处”,将Book.UNKNOWN_NUMBER_OF_PAGES其用作页码将解决此问题,但这只会导致 API 代码中出现 IndexOutOfBounds 异常。我已经考虑自己调用 print ,直到我得到NO_PAGE_EXISTS,但我需要一个具有适当页面尺寸的 Graphics 对象(我不知道如何获得它)。

最后:如果 Book 方法没有希望,我还能如何将多个 JTables(即多个 printables)的输出组合到一个作业中?我考虑将表格导出为 PDF,但 JTable 的内置分页非常好,我宁愿不必自己做。我最后的手段是放弃并使用 iText 的内置表格函数来构建表格的副本。

编辑 3:根据我在下面的评论,我通过生成一个可打印的、确定页数,然后生成一个新的来完成这项工作。我还修改了 Durendal 的包装器,以免除我们遍历每一页的麻烦。包装器的代码是

    class PrintableWrapper implements Printable
    {
        private Printable delegate;
        private int offset;

        public PrintableWrapper(Printable delegate, int offset) {
            this.offset = offset;
            this.delegate = delegate;
        }

        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            return delegate.print(graphics, pageFormat, pageIndex-offset);
        }
    }   

我把 Durendal 的用于确定页数的代码放在它自己的函数中

    public int getNumberOfPages(Printable delegate, PageFormat pageFormat) throws PrinterException 
    {
        Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics();
        int numPages = 0;
        while (true) {
            int result = delegate.print(g, pageFormat, numPages);
            if (result == Printable.PAGE_EXISTS) {
                ++numPages;
            } else {
                break;
            }
        }

        return numPages;
    }

创建图书对象后,我的打印代码如下所示

            int totalPages = 0;
            for (DragNDropTable t : getAllTables() )
            {
                int pages = getNumberOfPages(t.getPrintable(PrintMode.FIT_WIDTH, null, null), pf);
                Printable p = t.getPrintable(PrintMode.FIT_WIDTH, null, null);
                printJob.append(new PrintableWrapper(p,totalPages), pf, pages);
                totalPages += pages;
            }
            printer.setPageable(printJob);

            if (printer.printDialog())
                printer.print();

它就像一个魅力!

编辑2:(你可以跳过这个)我试过Durendal的回答。当我打印足够多的页面时,多页打印件会多次打印最后一页(打印件中的每一页打印一次)。这与我在第一次编辑(如下)中讨论的问题相同,我不知道为什么会发生这种情况,我的调试语句说它正在正确打印所有页面,但是打印了多页可打印的最后一页代替每一页。附上代码。洞察力受到赞赏(并以虚拟 cookie 回报)

try {       
    PrinterJob printer = PrinterJob.getPrinterJob();

    //Set 1/2 " margins and orientation
    PageFormat pf = printer.defaultPage();
    pf.setOrientation(PageFormat.LANDSCAPE);
    Paper paper = new Paper();
    double margin = 36; // half inch
    paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2);
    pf.setPaper(paper);

    Book printJob = new Book();

    // Note for next line: getAllTables() returns an ArrayList of JTables
    for (JTable t : getAllTables() )  
    {
        Printable p = t.getPrintable(PrintMode.FIT_WIDTH, null, null);
        int pages = getNumberOfPages(p, pf);

        for (int i=0; i < pages; i++)
            printJob.append(new PageWrapper(p,i), pf);
    }

    printer.setPageable(printJob);

    System.out.println(printJob.getNumberOfPages());

    if (printer.printDialog())
        printer.print();
    } catch (PrinterException e) {
        e.printStackTrace();
}

public int getNumberOfPages(PageFormat pageFormat) throws PrinterException 
{
    Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics();
    int numPages = 0;
    while (true) {
        int result = delegate.print(g, pageFormat, numPages);
        if (result == Printable.PAGE_EXISTS) 
            ++numPages;
        else
            break;
    }

    return numPages;
}

我正在使用 Durendal 在下面给出的未修改的 PageWrapper。

编辑 1:(你可以跳过这个)与 Durendal 的回答相吻合,我试图制作一个包装器,让我们不必自己迭代页面。不幸的是,它似乎无法在多页打印件上正常工作,在文档中多次打印同一页。我发布它,只是因为有人可以让它工作,而且使用起来稍微方便一些。

class PrintableWrapper implements Printable
    {
        private Printable delegate;
        private int offset;

        public PrintableWrapper(Printable delegate, int offset) {
            this.offset = offset;
            this.delegate = delegate;
        }

        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            return delegate.print(graphics, pageFormat, pageIndex-offset);
        }


        public int getNumberOfPages(PageFormat pageFormat) throws PrinterException 
        {
            Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics();
            int numPages = 0;
            while (true) {
                int result = delegate.print(g, pageFormat, numPages);
                if (result == Printable.PAGE_EXISTS) 
                    ++numPages;
                else
                    break;
            }

            return numPages;
        }
    }

我的打印代码现在看起来像这样(在我设置页面格式之后)

Book printJob = new Book();
int totalPages = 0;

for (DragNDropTable t : getAllTables() )
{
    Printable p = t.getPrintable(PrintMode.FIT_WIDTH, null, null);
    PrintableWrapper pw = new PrintableWrapper(p, totalPages);
    totalPages += pw.getNumberOfPages(pf);
    printJob.append(pw, pf,pw.getNumberOfPages(pf));
}

printer.setPageable(printJob);

if (printer.printDialog())
{
    printer.print();

}
4

1 回答 1

3

I had the very same Problem recently. The Book class by itself is useless, because if you add Printables to it, when the Book is printed it will pass the pageIndex from the Book to the Printable at pageIndex.

That is in most cases not what you want.

Create a simple Printable Wrapper that can remember a pageIndex to be used for the Printable delegate and add those to the Book:

class PageWrapper implements Printable {
    private Printable delegate;
    private int localPageIndex;

    public PageWrapper(Printable delegate, int pageIndex) {
        this.localPageIndex = pageIndex;
        this.delegate = delegate;
    }

    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        return delegate.print(graphics, pageFormat, localPageIndex);
    }
}

Now you need to iterate trough each page of each Printable/Pageable and add a wrapper instance (that knows the pageIndex into its delegate) to the Book. That solves the problem that Book passes the wrong pageIndex to the printables added to it (its easier for Pageables than Printables).

You can detect the number of Pages in a Printable by printing it ;) Yes, I'm serious:

Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics();
int numPages = 0;
while (true) {
    int result = printable.print(g, pageFormat, numPages);
    if (result == Printable.PAGE_EXISTS) {
        ++numPages;
    } else {
        break;
    }
}

Its important you obtain your PageFormat instance from the actual PrinterJob you want to print to, because the number of pages depends on the page format (paper size).

于 2013-02-08T15:47:57.117 回答