将多个文档添加到PrinterJob

时间:2013-05-23 13:03:43

标签: java pdf printing

我使用以下方法在我正在使用的程序中打印pdf文件。它有效,但每次我想打印一个页面时都要调用该方法。因此,如果我想要打印相同的文档五次,则整个方法必须执行五次。我的问题是,有没有办法在PrinterJob中添加多个文档,这样可以只调用一次这个方法来打印出我需要的内容?

public static void printPdf(File thePdf)
{
    File f = thePdf;
    RandomAccessFile fis = null;
    FileChannel fc = null;
    ByteBuffer bb = null;
    try 
    {
        PrintService  service = PrintServiceLookup.lookupDefaultPrintService();

        fis = new RandomAccessFile(f, "rw");
        fc = fis.getChannel();
        bb = ByteBuffer.allocate((int) fc.size());
        fc.read(bb);
        PDFFile pdfFile = new PDFFile(bb); 
        PDFPrintPage pages = new PDFPrintPage(pdfFile);
        PrinterJob pjob = PrinterJob.getPrinterJob();
        pjob.setPrintService(service);

        PageFormat pf = PrinterJob.getPrinterJob().defaultPage();

        pf.setOrientation(PageFormat.PORTRAIT);

        Paper paper = new Paper();

        paper.setImageableArea(0, 0, paper.getWidth() * 2, paper.getHeight());

        pf.setPaper(paper);

        pjob.setJobName(f.getName());

        Book book = new Book();
        book.append(pages, pf, pdfFile.getNumPages());
        pjob.setPageable(book);
        pjob.print();

    } 
    catch (IOException|PrinterException e) 
    {
        ShowErrors.show_errors("Printing exception: "+e.toString());
    } 
    finally 
    {
        try
        {
            if (fc != null) 
                fc.close();
            if (fis != null) 
                fis.close();
        }
        catch (IOException e) 
        {
            System.out.println("Exception closing IO channel: "+e.toString());
        }

        if (bb != null) 
        {
            bb.clear();
        }
    }
}

如果我可以将这个方法传递给一个File对象数组并将每个对象添加到pjob(我猜这就是你要添加它们的地方,如果可能的话),这将是完美的。我仔细查看了文档,老实说这对我来说很困惑。如果有人能指出我正确的方向,我会很感激。感谢。

1 个答案:

答案 0 :(得分:0)

由于您的代码无法运行,因此我最好猜测打印多个副本的文件。

public void printPdf(List<File> pdfFiles, int copies) {
    PrintService service = PrintServiceLookup.lookupDefaultPrintService();
    RandomAccessFile fis = null;
    FileChannel fc = null;
    ByteBuffer bb = null;
    try {

        for (int i = 0; i < pdfFiles.size(); i++) {
            File f = pdfFiles.get(i);
            fis = new RandomAccessFile(f, "rw");
            fc = fis.getChannel();
            bb = ByteBuffer.allocate((int) fc.size());
            fc.read(bb);

            for (int j = 0; j < copies; j++) {
                PrinterJob pjob = PrinterJob.getPrinterJob();
                pjob.setPrintService(service);

                PageFormat pf = PrinterJob.getPrinterJob().defaultPage();
                pf.setOrientation(PageFormat.PORTRAIT);

                Paper paper = new Paper();
                paper.setImageableArea(0, 0, paper.getWidth() * 2,
                        paper.getHeight());
                pf.setPaper(paper);

                PDFFile pdfFile = new PDFFile(bb);
                PDFPrintPage pages = new PDFPrintPage(pdfFile);
                pjob.setJobName(f.getName());

                Book book = new Book();
                book.append(pages, pf, pdfFile.getNumPages());
                pjob.setPageable(book);
                pjob.print();
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    } catch (PrinterException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fc != null)
                fc.close();
            if (fis != null)
                fis.close();
        } catch (IOException e) {
            System.out.println("Exception closing IO channel: "
                    + e.toString());
        }

        if (bb != null) {
            bb.clear();
        }
    }
}