java pdfbox 打印pdf_PDFBox:如何使用指定的打印机打印pdf?

我想使用PDFBox打印 由iText创建的 PDF文件

。我已经使用PDDocument类及其方法print()成功尝试了此操作。您可以在此处找到文档:http

(我正在使用此代码:)

public static void printPDF(String fileName)

throws IOException, PrinterException {

PDDocument doc = PDDocument.load(fileName);

doc.print();

}

方法print()很好用,但是 有一个问题:当我需要打印多个文件时,该方法要求我为每个文档选择打印机。

有什么办法只能设置一次打印机吗?

对于打印机选择,我可以使用以下代码作为示例:

public static PrintService choosePrinter() {

PrinterJob printJob = PrinterJob.getPrinterJob();

if(printJob.printDialog()) {

return printJob.getPrintService();

}

else {

return null;

}

}

提前致谢

解:

public static PrintService choosePrinter() {

PrinterJob printJob = PrinterJob.getPrinterJob();

if(printJob.printDialog()) {

return printJob.getPrintService();

}

else {

return null;

}

}

public static void printPDF(String fileName, PrintService printer)

throws IOException, PrinterException {

PrinterJob job = PrinterJob.getPrinterJob();

job.setPrintService(printer);

PDDocument doc = PDDocument.load(fileName);

doc.silentPrint(job);

}

### 回答1: 下面是一个使用PDFBox框架调用打印机打印PDF文件的具体代码示例:// 加载PDF文件 PDDocument document = PDDocument.load(new File("example.pdf"));// 获取默认打印机 PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();// 创建打印作业 PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintService(defaultPrintService);// 设置打印参数 PageFormat pageFormat = job.defaultPage(); Paper paper = pageFormat.getPaper(); double margin = 2.0; paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2); pageFormat.setPaper(paper); job.setPrintable(new PDFPrintable(document, Scaling.SHRINK_TO_FIT), pageFormat);// 开始打印 job.print(); ### 回答2: 使用PDFBox框架调用打印机打印PDF文件的具体代码案例如下: 首先,确保你已经在项目中添加了PDFBox的依赖。 ```java import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.printing.PDFPageable; import javax.print.*; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.standard.PrinterName; import java.awt.print.PrinterJob; import java.io.FileInputStream; import java.io.IOException; public class Main { public static void main(String[] args) { String pdfFilePath = "路径/文件名.pdf"; printPDF(pdfFilePath); } public static void printPDF(String pdfFilePath) { try (PDDocument document = PDDocument.load(new FileInputStream(pdfFilePath))) { PrinterJob job = PrinterJob.getPrinterJob(); job.setPageable(new PDFPageable(document)); PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null); PrintService printService = null; // 根据打印机名称选择打印机 for (PrintService service : printServices) { if (service.getName().contains("打印机名称")) { printService = service; break; } } // 设置打印机 job.setPrintService(printService); // 设置打印属性 HashPrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet(); attributes.add(new PrinterName(printService.getName(), null)); // 执行打印 job.print(attributes); } catch (IOException | PrinterException e) { e.printStackTrace(); } } } ``` 在上述代码中,我们首先加载PDF文件,然后创建一个`PrinterJob`对象,将PDF文件与`PDFPageable`对象关联。接下来,我们使用`PrintServiceLookup`来获取所有可用的打印机列表,并根据打印机名称选择需要使用打印机。然后,我们设置打印机打印属性,并执行打印操作。 请注意,你需要将代码中的`pdfFilePath`替换为你的PDF文件的路径和文件名,并将代码中的`打印机名称`替换为你需要使用打印机的名称。 希望这个例子能帮助到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值