如何在Java中打印PDF文件
通过 Java 应用程序以编程方式打印 PDF,可实现文档处理自动化和打印功能的无缝集成。 使用 IronPDF for Java,您可以将 PDF 直接发送到物理打印机,对副本、页面范围等打印设置进行精确控制。 本指南演示了如何使用 IronPDF 的功能来简化 Java 应用程序中的打印任务。
如何在Java中打印PDF文件
- 安装Java库以打印PDF文件
- 加载现有 PDF 或渲染新 PDF
- 使用
print
方法通过对话框打印 - 使用
printWithoutDialog
方法无需对话框进行打印 - 查看打印的 PDF 文档
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 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();