如何在 Java 中打印 PDF 文件

如何使用 Java 列印 PDF 文件

This article was translated from English: Does it need improvement?
Translated
View the article in English

要在 Java 中列印 PDF 文件,可以使用IronPDF庫,該庫提供了一些簡單的方法,例如 printWithoutDialog() 用於立即列印,以及 print() 用於向使用者顯示列印對話方塊選項。

快速入門:在 Java 中列印 PDF 檔案

```java :title=快速入門 // 將IronPDF依賴項新增到您的 Maven pom.xml 檔案中 // 導入IronPDF類 import IronPDF.*;

// 載入 PDF 文檔 PdfDocument pdf = new PdfDocument(Paths.get("MyPdf.pdf"));

// 不顯示對話方塊列印 pdf.printWithoutDialog();

// 或顯示列印對話框 pdf.print();


## Java中的PDF列印是什麼?

PDF 在 Java 應用程式中非常重要,因為它允許開發人員以平台無關的方式建立和操作 PDF 文件。 PDF 格式廣泛用於儲存和共用文檔,因此對於處理文件管理或基於文件的工作流程的 Java 應用程式來說至關重要。

Java 中的 PDF 列印是指以程式設計方式將 PDF 文件傳送到實體印表機或虛擬印表機。 此功能對於自動化文件列印工作流程、產生發票、報告或其他可列印文件的商業應用程式至關重要。 Java 開發人員經常在桌面應用程式、伺服器端應用程式或批次系統中實作 PDF 列印功能。

Java中有多種方法可以產生和列印PDF文件。 一種常見的方法是使用提供用於建立和操作 PDF 文件的類別的庫。 本指南展示如何在 Java 應用程式中使用IronPDF庫產生和列印 PDF 文件。 有關 IronPDF 功能的完整文檔,請造訪"[入門概述"](https://ironpdf.com/java/docs/) 。

<hr>

<div class="hsg-featured-snippet">
<h3>如何在Java中列印PDF文件</h3>
<ol>
<li><a class="js-modal-open" data-modal-id="download-modal" href="#download-modal">安裝 Java 庫以列印 PDF 文件</a></li>
<li>使用<a href="#anchor-load-a-pdf-in-a-java-application">`PdfDocument`</a>類別載入現有 PDF</li>
<li>呼叫<a href="#anchor-print-a-pdf-document-with-default-settings">`printWithoutDialog`</a>方法可立即列印</li>
<li>配置印表機設定以實現自訂輸出</li>
<li>使用<a href="#anchor-the-print-dialog">`print`</a>方法列印 PDF 檔案以顯示對話框</li>
</ol>
</div>

## 什麼是IronPDF Java PDF 函式庫?

IronPDF是一個 Java 函式庫,用於產生、操作和轉換 PDF 文件。 它基於[IronPDF C# .NET庫](/),為.NET平台提供類似的功能。

IronPDF提供了一個用於處理 PDF 文件的高級 API,使開發人員能夠處理 PDF 文件而無需處理底層文件格式細節。 它支援常見的 PDF 操作,例如建立新文件、新增內容、格式化文字、合併 PDF 文件和分割 PDF 文件。

IronPDF支援將 HTML、CSS 和JavaScript程式碼轉換為 PDF,因此可以輕鬆地從網頁或 HTML 範本產生 PDF 檔案。 它還提供PDF列印功能。 該庫處理 PDF 生成和列印的所有複雜操作,提供了一個簡單直觀的 API,Java 開發人員可以輕鬆地將其整合到他們的應用程式中。 了解更多關於如何使用 IronPDF 的全面功能[在 Java 中建立 PDF 的資訊](https://ironpdf.com/java/how-to/java-create-pdf-tutorial/)。

## 如何使用IronPDF Java 列印 PDF 文件?

### 需要具備哪些先決條件?

要在 Java 中列印 PDF 文件,您需要:

1. Eclipse IDE 或任何其他 Java IDE(IntelliJ IDEA、NetBeans 等)
2. 在 Eclipse 或其他 IDE 中執行的 Maven 項目
3. 需要穩定的網路連線才能安裝IronPDF Java 庫
4. 已安裝 Java 開發工具包 (JDK) 8 或更高版本
5. 用於測試的 PDF 文件

### 如何在 Maven 專案中安裝IronPDF庫?

若要在 Maven 專案中安裝IronPDF ,請將IronPDF依賴項新增至專案的**pom.xml**檔案中。

將以下相依性新增至 pom.xml 檔案的 `<dependencies>` 部分:

```xml
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>YOUR_VERSION_HERE</version>
</dependency>

將依賴項新增至 pom.xml 檔案後,在終端機中執行 mvn install 命令,或按 Ctrl+S 下載IronPDF並將其安裝到您的 Maven 專案中。 Maven 建置系統會自動下載所有必要的依賴項,並使它們可供您的專案使用。

在使用IronPDF之前,請在 src 資料夾中的主 App.java 原始檔中匯入IronPDF類別。

IDE package explorer showing ironpdf-java project structure with src/main/java and App.java file

IronPDF for Java 的套件瀏覽器樹

開啟"App.java"文件,並使用以下導入語句新增IronPDF包:

import IronPDF.*;
import IronPDF.*;
JAVA

如何在Java應用程式中載入PDF檔案?

IronPDF for Java 提供了一個建構函數,用於將 PDF 內容載入到庫中。 有效參數包括位元組數組和檔案路徑。 對於受密碼保護的文檔,可以提供包含密碼的第三個參數。

以下程式碼片段載入位於檔案系統中的 PDF 檔案:

// Set the license key for IronPDF
// Learn more about licensing at https://ironpdf.com/java/get-started/license-keys/
License.setLicenseKey("Enter-Your-License");  

// Load PDF from the filesystem
PdfDocument pdf = new PdfDocument(Paths.get("MyPdf.pdf"));

// Alternative: Load from byte array
// byte[] pdfBytes = Files.readAllBytes(Paths.get("MyPdf.pdf"));
// PdfDocument pdfFromBytes = new PdfDocument(pdfBytes);

// For password-protected PDFs
// PdfDocument protectedPdf = new PdfDocument(Paths.get("Protected.pdf"), "password123");
// Set the license key for IronPDF
// Learn more about licensing at https://ironpdf.com/java/get-started/license-keys/
License.setLicenseKey("Enter-Your-License");  

// Load PDF from the filesystem
PdfDocument pdf = new PdfDocument(Paths.get("MyPdf.pdf"));

// Alternative: Load from byte array
// byte[] pdfBytes = Files.readAllBytes(Paths.get("MyPdf.pdf"));
// PdfDocument pdfFromBytes = new PdfDocument(pdfBytes);

// For password-protected PDFs
// PdfDocument protectedPdf = new PdfDocument(Paths.get("Protected.pdf"), "password123");
JAVA

如何使用預設設定列印PDF文件?

IronPDF提供兩種列印 PDF 檔案的方法。 第一種方法是使用預設印表機和頁面設定立即列印文件。 使用 printWithoutDialog 方法執行此操作。 這種方法適用於不需要使用者互動的自動化批次列印場景。

// Print PDF document using default printer settings without showing a print dialog
// This will immediately send the document to the default system printer
pdf.printWithoutDialog();

// The method returns immediately after sending the print job to the spooler
System.out.println("PDF document sent to default printer");
// Print PDF document using default printer settings without showing a print dialog
// This will immediately send the document to the default system printer
pdf.printWithoutDialog();

// The method returns immediately after sending the print job to the spooler
System.out.println("PDF document sent to default printer");
JAVA

這種方法非常適合伺服器端應用程式、自動化工作流程,或當您想要提供快速列印選項而無需使用者輸入時。 對於更進階的列印場景,請查看文件中的PDF 列印範例

什麼是列印對話框?

第二種方法允許使用者在列印前指定列印選項。 使用 print 方法來實現此功能。 此方法會顯示標準系統列印對話框,使用戶能夠完全控制列印過程。

// Display print dialog to let the user specify printing options
// This will show the system print dialog with all available printers
pdf.print();

// The method will wait for user interaction before proceeding
// User can select printer, paper size, orientation, number of copies, etc.
// Display print dialog to let the user specify printing options
// This will show the system print dialog with all available printers
pdf.print();

// The method will wait for user interaction before proceeding
// User can select printer, paper size, orientation, number of copies, etc.
JAVA

呼叫此方法時,將出現列印對話框窗口,允許使用者更改印表機、設定紙張尺寸、更改份數等。 這種方法非常適合需要使用者控制列印選項的桌面應用程式。 了解更多關於使用其他選項列印到實體印表機的資訊

Windows Print dialog with Microsoft Print to PDF selected, showing page range, copies, and print options

使用`print()`方法執行程式後,會顯示列印對話方塊。

完整的原始碼是什麼?

本指南中使用的完整來源文件如下:

package IronPDF.ironpdf_java;

// Import statement for IronPDF Java
import IronPDF.*;
import java.awt.print.PrinterException;
import java.io.IOException;
import java.nio.file.Paths;

public class App {
    public static void main(String[] args) throws PrinterException, IOException {
        // Apply your license key
        // Get your license key from https://ironpdf.com/java/get-started/license-keys/
        License.setLicenseKey("Enter-Your-License");

        // 載入 PDF 文檔 from the file system
        PdfDocument pdf = new PdfDocument(Paths.get("MyPdf.pdf"));

        // Option 1: Print the PDF document without displaying a print dialog
        // Uses default printer and settings
        pdf.printWithoutDialog();

        // Option 2: Display the print dialog for the user to configure printing options
        // Allows selection of printer, copies, page range, etc.
        pdf.print();

        // Don't forget to close the document when done
        pdf.close();
    }
}
package IronPDF.ironpdf_java;

// Import statement for IronPDF Java
import IronPDF.*;
import java.awt.print.PrinterException;
import java.io.IOException;
import java.nio.file.Paths;

public class App {
    public static void main(String[] args) throws PrinterException, IOException {
        // Apply your license key
        // Get your license key from https://ironpdf.com/java/get-started/license-keys/
        License.setLicenseKey("Enter-Your-License");

        // 載入 PDF 文檔 from the file system
        PdfDocument pdf = new PdfDocument(Paths.get("MyPdf.pdf"));

        // Option 1: Print the PDF document without displaying a print dialog
        // Uses default printer and settings
        pdf.printWithoutDialog();

        // Option 2: Display the print dialog for the user to configure printing options
        // Allows selection of printer, copies, page range, etc.
        pdf.print();

        // Don't forget to close the document when done
        pdf.close();
    }
}
JAVA

有關可用列印方法和選項的更多詳細信息,請參閱IronPDF API 參考

PDF列印的其他注意事項

在Java應用程式中實作PDF列印時,請考慮以下重要因素:

錯誤處理

列印PDF文件時,請務必實施適當的錯誤處理。 印表機可能離線、缺紙或出現其他問題:

try {
    pdf.printWithoutDialog();
} catch (PrinterException e) {
    System.err.println("Printing failed: " + e.getMessage());
    // Handle the error appropriately
}
try {
    pdf.printWithoutDialog();
} catch (PrinterException e) {
    System.err.println("Printing failed: " + e.getMessage());
    // Handle the error appropriately
}
JAVA

效能最佳化

對於大型 PDF 文件或批量列印操作,請考慮在列印前壓縮 PDF 文件,以減少記憶體使用並提高列印速度。

跨平台相容性

IronPDF 的列印功能可在不同的作業系統(Windows、Linux、macOS)上執行,但不同平台的印表機配置和可用選項可能有所不同。 務必在目標部署平台上測試列印功能。

了解更多關於使用IronPDF庫在 Java 中進行 PDF 列印的資訊

為什麼我應該使用IronPDF進行 PDF 列印?

IronPDF是一個功能強大且易於使用的程式庫,可在 Java 應用程式中列印 PDF 檔案。 IronPDF擁有全面的功能和豐富的文檔,簡化了產生和自訂用於列印或共享的專業級 PDF 的過程。 無論是建立發票、報告或其他文檔, IronPDF都能提供您所需的工具。

IronPDF提供與現有 Java 應用程式的無縫整合、強大的錯誤處理和跨平台相容性。 此函式庫的 API 設計簡潔,只需幾行程式碼即可實現 PDF 列印功能,同時在需要時還能提供對進階功能的存取。

IronPDF提供免費試用版,供在生產環境中進行測試。 價格從 $799 起。 Give IronPDF a try and see how it can streamline your PDF printing workflow.

常見問題解答

如何在 Java 中列印 PDF 檔案而不顯示列印對話框?

要在 Java 中打印 PDF 文件而不顯示對話框,請使用 IronPDF for Java 的 printWithoutDialog() 方法。首先,使用 PdfDocument 類載入 PDF 文件,然後只需呼叫 pdf.printWithoutDialog() 即可立即列印至預設印表機。

print() 和 printWithoutDialog() 方法有什麼不同?

IronPDF 提供了兩種列印方法:print() 會顯示列印對話框,讓使用者在列印前選擇印表機設定和偏好設定;而 printWithoutDialog() 則會直接將文件傳送到預設印表機,不需要使用者互動,非常適合自動化批次列印。

如何在 Java 專案中加入 IronPDF 以進行 PDF 列印?

若要在 Java 專案中加入 IronPDF,請在 Maven pom.xml 檔案中將其列為依賴項目。新增後,匯入必要的 IronPDF 類別 (import com.ironsoftware.ironpdf.*) 以在您的 Java 應用程式中存取 PDF 列印功能。

在 Java 中列印 PDF 時,是否可以設定印表機設定?

是的,IronPDF 允許您配置印表機設定,以便在列印 PDF 時進行自訂輸出。您可以指定各種列印參數和選項,以控制 PDF 文件的列印方式,確保它們符合您的特定要求。

是否可以在伺服器端 Java 應用程式中列印 PDF?

是的,IronPDF 支持服务器端 Java 應用程序中的 PDF 打印。它通常用於批次處理系統和自動化文件工作流程,在這些系統和流程中,PDF 需要在無需使用者互動的情況下進行列印,因此它非常適合於生成發票、報告和其他文件的企業應用程式。

PDF 列印支援哪些類型的印表機?

IronPDF 支援列印至實體和虛擬印表機。這種靈活性讓您可以將 PDF 文件傳送至標準辦公室印表機、網路印表機或虛擬 PDF 印表機進一步處理,使其適用於各種商業情境和文件管理工作流程。

Darrius Serrant
全棧軟件工程師 (WebOps)

Darrius Serrant 擁有邁阿密大學計算機科學學士學位,目前任職於 Iron Software 的全栈 WebOps 市場營銷工程師。從小就迷上編碼,他認為計算既神秘又可接近,是創意和解決問題的完美媒介。

在 Iron Software,Darrius 喜歡創造新事物,並簡化複雜概念以便於理解。作為我們的駐場開發者之一,他也自願教學生,分享他的專業知識給下一代。

對 Darrius 來說,工作令人滿意因為它被重視且有實際影響。

準備好開始了嗎?
版本: 2026.3 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明?
執行範例 觀看您的 HTML 變成 PDF。