如何在 Java 中列印 PDF 檔案

Darrius Serrant
Darrius Serrant
2024年10月21日
已更新 2024年12月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)

Darrius Serrant 擁有邁阿密大學的計算機科學學士學位,目前擔任 Iron Software 的全端 WebOps 行銷工程師。自幼對編程產生興趣,他認為計算機既神秘又易於接觸,使其成為創造力和解決問題的完美媒介。

在 Iron Software,Darrius 享受創造新事物並簡化複雜概念使其更易理解的過程。作為我們的其中一位常駐開發人員,他也自願教導學生,將他的專業知識傳授給下一代。

對 Darrius 來說,他的工作之所以令人滿足,是因為它受到重視並且產生了真正的影響。