gpt4 book ai didi

java - 将多个文档添加到 PrinterJob

转载 作者:行者123 更新时间:2023-11-29 09:02:09 29 4
gpt4 key购买 nike

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

最佳答案

由于您的代码不可运行,所以这是我最好的猜测,即打印多个副本的多个文件。

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();
}
}
}

关于java - 将多个文档添加到 PrinterJob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16714707/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com