次の方法を使用して、使用しているプログラムで 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 に追加できれば完璧です (可能であれば、そこに追加すると思います)。私はドキュメントに目を通しましたが、正直なところ、かなり混乱しています。誰かが私を正しい方向に向けることができれば、私はそれを感謝します. ありがとう。