2

我使用以下方法在我正在使用的程序中打印 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 中,那将是完美的(如果可能的话,我猜这就是您要添加它们的地方)。我浏览了文档,老实说,这让我很困惑。如果有人能指出我正确的方向,我将不胜感激。谢谢。

4

1 回答 1

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();
        }
    }
}
于 2013-05-23T15:07:07.940 回答