如何在 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();