2

次の方法を使用して、使用しているプログラムで pdf ファイルを印刷します。動作しますが、1 ページを印刷するたびにメソッドを呼び出す必要があります。したがって、同じドキュメントを 5 回印刷する場合は、このメソッド全体を 5 回実行する必要があります。私の質問は、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 に答える