从Java打印多个PDF作为单个打印作业(物理打印) [英] Printing Multiple PDFs from Java as a single print job (physical printing)

查看:186
本文介绍了从Java打印多个PDF作为单个打印作业(物理打印)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个打印作业中从java(使用java打印服务)打印多个pdf。

I would like to print multiple pdfs from java (using the java print service) in a single print job.

我想将多个pdf作为单个作业发送到打印机。这样,当我从打印机中取出时,我的批处理中的所有文档一起打印并且不会与其他人的打印作业交错。

I would like to send multiple pdfs as a single job to the printer. This is so that all the documents in my 'batch' print together and are not interleaved with someone else's print jobs when I go pick them up from the printer.

批处理可能包含1000个打印作业。

A batch potentially consists of 1000s of print jobs.

我试过jpedal,但它不支持java.awt.print.Book

I tried jpedal, but it does not support java.awt.print.Book

        Book book = new Book();
        PdfDecoder pdfDecoder = readFileApplyOptions("C:/Temp/singlepagetest.pdf", pageFormat);
        book.append(pdfDecoder, pageFormat);

        PdfDecoder pdfDecoderTwo = readFileApplyOptions("C:/Temp/printfax-test.pdf",pageFormat);
        book.append(pdfDecoderTwo, pageFormat);

        printJob.setPageable(book);
        printJob.print();

仅打印出第一个pdf。如何在单个作业中打印多个pdf?

only prints out the first pdf. How do I print multiple pdfs in a single job?

readFileAndApplyOptions()基本上创建一个新的PdfDecoder对象并返回它。

readFileAndApplyOptions() basically creates a new PdfDecoder object and returns it.

我还尝试了Sun的PDFRenderer PDFRenderer 以类似的方式(使用Book对象),但我的代码仍然只打印出第一页。

I also tried Sun's PDFRenderer PDFRenderer in a similar fashion (using the Book object), but my code still only prints out the first page only.

之前是否有人遇到类似的问题?有没有我可能缺少的解决方案?

Has anyone encountered a similar issue before? Is there a solution I might be missing?

推荐答案

我在JTabbedPane的几个JPanel上打印时遇到了同样的困难,每个在另一页上。我把它们收集在一本书中,但它只打印第一页。

I met the same difficulties when printing at once several JPanels of a JTabbedPane, each on a separate page. I gather them in a Book but it only prints the first page.

Book类运行良好(正确的页数),但我认为问题来自setPageable。由于一本书不是可打印的,我做到了,它有效!

The Book class works well (right number of pages), but I suppose the problem comes from setPageable. Since a Book is not a Printable, I made it, and it works !

解决方法:


  1. 设计一个PrintableBook类:extends Book,实现Printable

  1. Design a PrintableBook class : extends Book, implements Printable

public class PrintableBook extends Book implements Printable {
    Vector<Printable> pages;// NB: we assume pages are single

    public PrintableBook() {
        super();
        pages = new Vector<Printable>();
    }

    public void add(Printable pp) {
        append(pp, pp.getPageFormat());
        pages.add(pp);
    }

    public int print(Graphics g, PageFormat pf, int pageIndex) {
        if (pageIndex >= pages.size())
            return NO_SUCH_PAGE;
        else {
            Printable pp = pages.elementAt(pageIndex);
            return pp.print(g, pf, 0);
        }
    }
}


  • 然后使用 printJob.setPrintable(printableBook)而不是 setPageable

    这篇关于从Java打印多个PDF作为单个打印作业(物理打印)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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