如何在Java中打印PDF文件

Darrius Serrant
Darrius Serrant
2024年十月21日
更新 2024年十二月17日
分享:
This article was translated from English: Does it need improvement?
Translated
View the article in English

通过 Java 应用程序以编程方式打印 PDF,可实现文档处理自动化和打印功能的无缝集成。 使用 IronPDF for Java,您可以将 PDF 直接发送到物理打印机,对副本、页面范围等打印设置进行精确控制。 本指南演示了如何使用 IronPDF 的功能来简化 Java 应用程序中的打印任务。

Print PDF

The first step is to load the PDF document you want to print. The print method opens the standard print dialog, allowing you to select the printer, page range, and other options before printing. Here's an example: javaimport com.ironsoftware.ironpdf.License;import com.ironsoftware.ironpdf.PdfDocument; License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01"); // Render HTML to PDFPdfDocument pdf = PdfDocument.renderHtmlAsPdf("<h1>测试</h1>"); // Print with Dialogpdf.print(); You will be prompted with a print dialog to select the printer and options, as shown below. Print Dialog


Print PDF without the Print Dialog

The printWithoutDialog method bypasses the print dialog and sends the document straight to the default printer. This is useful in automation scenarios where no user interaction is needed. javaimport com.ironsoftware.ironpdf.License;import com.ironsoftware.ironpdf.PdfDocument; License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01"); // Render HTML to PDFPdfDocument pdf = PdfDocument.renderHtmlAsPdf("<h1>测试</h1>"); // Print without Dialogpdf.printWithoutDialog();

Darrius Serrant
全栈软件工程师(WebOps)

达瑞乌斯·塞兰特拥有迈阿密大学计算机科学学士学位,目前在Iron Software担任全栈WebOps营销工程师。从小对编码的热爱使他认为计算机既神秘又易接近,成为创意和解决问题的完美媒介。

在Iron Software,达瑞乌斯乐于创造新事物并简化复杂概念,使其更易于理解。作为我们在职开发者之一,他还自愿教授学生,将他的专业知识传授给下一代。

对达瑞乌斯而言,他的工作之所以令人满足,是因为它具有价值并产生了真正的影响。