如何在 Java 中列印 PDF 文件

如何使用Java列印PDF檔案

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

要在Java中列印PDF文件,請使用IronPDF程式庫,該程式庫提供像print()使用者選擇列印對話框選項的方法。

快速入門:在Java中列印PDF文件

 :title=Quickstart
// Add IronPDF dependency to your Maven pom.xml file
// Import IronPDF classes
import com.ironsoftware.ironpdf.*;

// Load PDF document
PdfDocument pdf = new PdfDocument(Paths.get("MyPdf.pdf"));

// Print without dialog
pdf.printWithoutDialog();

// Or show print dialog
pdf.print();
 :title=Quickstart
// Add IronPDF dependency to your Maven pom.xml file
// Import IronPDF classes
import com.ironsoftware.ironpdf.*;

// Load PDF document
PdfDocument pdf = new PdfDocument(Paths.get("MyPdf.pdf"));

// Print without dialog
pdf.printWithoutDialog();

// Or show print dialog
pdf.print();
JAVA

什麼是Java中的PDF列印?

PDF在Java應用程式中十分重要,因為它讓開發者能夠以平台無關的方式建立和操作PDF文件。 PDF格式廣泛用於文件儲存和共享,這對於處理文件管理或基於文件工作流程的Java應用程式來說是必需的。

Java中的PDF列印涉及以編程方式將PDF文件發送到實體或虛擬印表機。 此功能對於那些自動化文件列印工作流程、生產發票、報告或其他可列印文件的商業應用程式至關重要。 Java開發者經常在桌面應用程式、伺服端應用程式或批處理系統中實施PDF列印功能。

用多種方法在Java中生成和列印PDF文件。 一個常見的方法是使用提供類來建立和操作PDF文件的程式庫。 本指南展示了如何在Java應用程式中使用IronPDF生成和列印PDF文件。 有關IronPDF功能的全面文件,請存取開始指南總覽


什麼是IronPDF Java PDF Library?

IronPDF是一個用於生成、操作和轉換PDF文件的Java程式庫。 它基於IronPDF C# .NET進行程式庫,提供.NET平台相似的功能。

IronPDF提供一個高層次的API來處理PDF文件,允許開發者在不需處理低層次的文件格式細節的情況下操作PDF文件。 它支持常見的PDF操作,如建立新文件、新增內容、格式化文字、合併PDF文件和分拆PDF文件。

IronPDF支持將HTML、CSS和JavaScript程式碼轉換成PDF,使得從網頁或HTML模板生成PDF文件變得容易。 它還提供PDF列印功能。 該程式庫處理所有PDF生成和列印的複雜性,提供了Java開發者可以輕鬆整合到其應用程式中的簡單直觀API。 使用IronPDF的綜合功能瞭解如何在Java中建立PDF

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

先決條件是什麼?

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

  1. Eclipse IDE或者任何其他Java IDE(如IntelliJ IDEA, NetBeans等)
  2. 在Eclipse或其他IDE中運行的Maven專案
  3. 穩定的網路連線來安裝IronPDF Java程式庫
  4. 已安裝Java Development Kit (JDK) 8或更高版本
  5. 用於測試的PDF文件

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

要在Maven專案中安裝IronPDF,請將IronPDF依賴項新增到專案的pom.xml文件中。

將以下依賴項新增到pom.xml文件中:

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

將依賴項新增到mvn install指令,或按Ctrl+S以下載並安裝IronPDF於您的Maven專案。 Maven建構系統會自動下載所有需要的依賴項,並使其在專案中可用。

在使用IronPDF之前,請導入主src/main/java資料夾中。

IDE 套件總管顯示 IronPDF-java 專案結構,含 src/main/java 與 App.java 檔案

IronPDF for Java的專案包瀏覽器樹

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

import com.ironsoftware.ironpdf.*;
import com.ironsoftware.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
Icon Quote related to 如何在Java應用程式中載入PDF?

我最喜歡的程式庫是IronPDF。它允許快速高效地操作PDF文件。它還有許多有價值的功能,例如導出到PDF/A格式和數位簽署PDF文件。

Milan Jovanovic related to 如何在Java應用程式中載入PDF?

Milan Jovanovic

Microsoft MVP

查看案例研究
Icon Quote related to 如何在Java應用程式中載入PDF?

IronOCR意味著我們每年可以從手動處理中節省$40,000,同時提高生產力,釋放資源以進行高影響的任務。我會強烈推薦它。

Brent Matzelle related to 如何在Java應用程式中載入PDF?

Brent Matzelle

首席技術官,OPYN

查看案例研究
Icon Quote related to 如何在Java應用程式中載入PDF?

IronSuite在我們的運營中扮演著至關重要的角色。這些工具增加了包括建立平面圖和改善庫存管理在內的業務效率。

David Jones related to 如何在Java應用程式中載入PDF?

David Jones

首席軟體工程師,Agorus Build

查看案例研究

如何使用預設設定列印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 列印對話方塊,已選取 Microsoft Print to PDF,顯示頁面範圍、份數與列印選項

程式運行後使用print()方法顯示列印對話框

完整源程式碼是什麼?

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

package IronPDF.ironpdf_java;

// Import statement for IronPDF Java
import com.ironsoftware.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");

        // Load PDF document 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 com.ironsoftware.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");

        // Load PDF document 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提供免費試用以在生產中測試。 定價從$999開始。 試用IronPDF,看看它如何簡化您的PDF列印流程。

常見問題

如何在 Java 中不顯示列印對話框列印 PDF 文件?

要在 Java 中列印 PDF 文件而不顯示對話框,請使用 IronPDF 的 printWithoutDialog() 方法。首先,使用 PdfDocument 類載入您的 PDF 文件,然後簡單地呼叫 pdf.printWithoutDialog() 立即列印到預設列印機。

print() 和 printWithoutDialog() 方法有什麼區別?

IronPDF 提供兩種列印方法:print() 顯示列印對話框,允許使用者選擇列印機設置和偏好,然後再列印,而 printWithoutDialog() 則是將文件直接發送到預設列印機,無需任何使用者交互,非常適合自動批量列印。

如何將 IronPDF 新增到我的 Java 項目中用於列印 PDF?

要將 IronPDF 新增到您的 Java 項目,請在 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.6 剛剛發布
Still Scrolling Icon

還在滾動嗎?

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