跳過到頁腳內容
使用 IRONPDF FOR JAVA

如何在 Java 中合併兩個 PDF 文件

本文將示範如何使用IronPDF Library for Java合併多個PDF文件。 我們將逐步介紹設置環境、導入程式庫、讀取輸入文件並將其合併成單個文件的過程。

IronPDF for Java

IronPDF for Java是一個強大的程式庫,允許開發者從頭創建新的PDF文件並將各種文件格式轉換為PDF文件。 它還提供了將多個PDF文件合併成單個文件的功能。

IronPDF for Java易於使用,具有簡單直觀的API,使開發者能夠輕鬆創建PDF文件。 它還支持在PDF中渲染圖表、處理PDF表單,甚至可程式化地處理數位簽章的方法。

先決條件

在實施之前,有幾個先決條件必須滿足才能完成PDF創建過程。

  1. 在您的系統上應安裝Java,並且其路徑應設置在環境變量中。 如果您還沒有安裝Java,請參考這篇Java網站的安裝指引獲取指導。
  2. 應安裝一個Java IDE,如Eclipse或IntelliJ。 您可以從這個官方Eclipse下載頁面下載Eclipse,並從JetBrains的下載區下載IntelliJ。
  3. 應下載IronPDF for Java程式庫並將其作為依賴項添加到您的專案中。 您可以在IronPDF官網上學習如何做到這一點。
  4. 在開始進行PDF轉換之前應安裝Maven並在您的IDE中進行整合。 如需有關安裝Maven並將其整合到您的環境中的教程,請訪問這篇JetBrains的Maven分步教程

IronPDF for Java 安裝

如果滿足所有要求,IronPDF for Java的安裝相當簡單且直接,即使對於Java新手來說也是如此。

在本文中,我們將使用JetBrains的IntelliJ IDEA進行樣本的安裝和運行。

首先,打開JetBrains IntelliJ IDEA並創建一個新的Maven專案。

如何使用Java合併兩個PDF文件,圖1:在IntelliJ中創建新Maven專案 在IntelliJ中創建新Maven專案

將會出現一個新窗口。 輸入專案的名稱,然後點擊完成。

如何使用Java合併兩個PDF文件,圖2:命名Maven專案並點擊完成 命名Maven專案並點擊完成

完成後,一個新專案將打開pom.xml以添加IronPDF for Java的Maven依賴項。

如何使用Java合併兩個PDF文件,圖3:pom.xml文件 pom.xml文件

pom.xml文件中添加以下依賴項,或者您可以從以下Maven Central上的IronPDF列表下載JAR文件。

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

一旦將依賴項放置在pom.xml文件中,文件的右上角將出現一個小圖標。

如何使用Java合併兩個PDF文件,圖4:點擊浮動圖標自動安裝Maven依賴項 點擊浮動圖標自動安裝Maven依賴項

點擊此圖標以安裝IronPDF for Java的Maven依賴項。 這將根據您的網速只需幾分鐘即可完成。

合併多個PDF文件

IronPDF允許您使用Java程式將多個PDF文件合併成一個PDF文件。 IronPDF提供多種合併PDF文件的方法:

  1. 創建兩個新的PDF文件並合併它們成為一個PDF。
  2. 將輸入的PDF文件打開為合併的PDF。
  3. 合併超過兩個PDF文件。

創建兩個新的PDF文件並將它們合併在一起

import com.ironsoftware.ironpdf.PdfDocument;

import java.io.IOException;
import java.nio.file.Paths;

// This class demonstrates how to create and merge two PDF documents using the IronPDF library.
public class Main {
    public static void main(String[] args) throws IOException {
        // Define the HTML content for the first PDF document
        String htmlA = "<p> [PDF_1] </p>"
                + "<p> Hi this is the first PDF </p>";
        // Define the HTML content for the second PDF document
        String htmlB = "<p> [PDF_2] </p>"
                + "<p> This is the 2nd PDF </p>";

        // Render the HTML content to create two separate PDF documents
        PdfDocument pdfA = PdfDocument.renderHtmlAsPdf(htmlA);
        PdfDocument pdfB = PdfDocument.renderHtmlAsPdf(htmlB);
        // Merge the two PDF documents into one
        PdfDocument merged = PdfDocument.merge(pdfA, pdfB);

        // Save the merged PDF document to the specified path
        merged.saveAs(Paths.get("assets/merged.pdf"));
    }
}
import com.ironsoftware.ironpdf.PdfDocument;

import java.io.IOException;
import java.nio.file.Paths;

// This class demonstrates how to create and merge two PDF documents using the IronPDF library.
public class Main {
    public static void main(String[] args) throws IOException {
        // Define the HTML content for the first PDF document
        String htmlA = "<p> [PDF_1] </p>"
                + "<p> Hi this is the first PDF </p>";
        // Define the HTML content for the second PDF document
        String htmlB = "<p> [PDF_2] </p>"
                + "<p> This is the 2nd PDF </p>";

        // Render the HTML content to create two separate PDF documents
        PdfDocument pdfA = PdfDocument.renderHtmlAsPdf(htmlA);
        PdfDocument pdfB = PdfDocument.renderHtmlAsPdf(htmlB);
        // Merge the two PDF documents into one
        PdfDocument merged = PdfDocument.merge(pdfA, pdfB);

        // Save the merged PDF document to the specified path
        merged.saveAs(Paths.get("assets/merged.pdf"));
    }
}
JAVA

如何使用Java合併兩個PDF文件,圖5:新PDF文件合併器 新PDF文件合併器

將現有文件合併為一個PDF

IronPDF允許您將現有的PDF文件合併為一個共同的PDF文件。只需指定PDF輸入文件的列表。 IronPDF將所有PDF文件合併成一個PDF文件並將其保存到目標文件。輸出將包含成功合併的PDF文件的結果。

import com.ironsoftware.ironpdf.PdfDocument;

import java.io.IOException;
import java.nio.file.Paths;

// This class demonstrates how to merge existing PDF files using the IronPDF library.
public class Main {
    public static void main(String[] args) throws IOException {
        // Load the existing PDF files from the specified paths
        PdfDocument pdfA = PdfDocument.fromFile(Paths.get("assets/1.pdf"));
        PdfDocument pdfB = PdfDocument.fromFile(Paths.get("assets/2.pdf"));
        // Merge the two PDF documents into one
        PdfDocument merged = PdfDocument.merge(pdfA, pdfB);

        // Save the merged PDF document to the specified path
        merged.saveAs(Paths.get("assets/merged.pdf"));
    }
}
import com.ironsoftware.ironpdf.PdfDocument;

import java.io.IOException;
import java.nio.file.Paths;

// This class demonstrates how to merge existing PDF files using the IronPDF library.
public class Main {
    public static void main(String[] args) throws IOException {
        // Load the existing PDF files from the specified paths
        PdfDocument pdfA = PdfDocument.fromFile(Paths.get("assets/1.pdf"));
        PdfDocument pdfB = PdfDocument.fromFile(Paths.get("assets/2.pdf"));
        // Merge the two PDF documents into one
        PdfDocument merged = PdfDocument.merge(pdfA, pdfB);

        // Save the merged PDF document to the specified path
        merged.saveAs(Paths.get("assets/merged.pdf"));
    }
}
JAVA

如何使用Java合併兩個PDF文件,圖6:現有码PDF合併器輸出 現有码PDF合併器輸出

合併兩個以上的PDF文件

您可以輕鬆地使用IronPDF for Java合併兩個以上的PDF文件。

import com.ironsoftware.ironpdf.PdfDocument;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

// This class demonstrates how to merge more than two PDF documents using the IronPDF library.
public class Main {
    public static void main(String[] args) throws IOException {
        // Create a list to hold the PDF documents
        List<PdfDocument> pdfList = new ArrayList<>();
        // Add existing PDF files to the list
        pdfList.add(PdfDocument.fromFile(Paths.get("assets/1.pdf")));
        pdfList.add(PdfDocument.fromFile(Paths.get("assets/2.pdf")));
        pdfList.add(PdfDocument.fromFile(Paths.get("assets/3.pdf")));
        // Merge all PDF documents in the list into one
        PdfDocument merged = PdfDocument.merge(pdfList);
        // Save the merged PDF document to the specified path
        merged.saveAs(Paths.get("assets/merged.pdf"));
    }
}
import com.ironsoftware.ironpdf.PdfDocument;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

// This class demonstrates how to merge more than two PDF documents using the IronPDF library.
public class Main {
    public static void main(String[] args) throws IOException {
        // Create a list to hold the PDF documents
        List<PdfDocument> pdfList = new ArrayList<>();
        // Add existing PDF files to the list
        pdfList.add(PdfDocument.fromFile(Paths.get("assets/1.pdf")));
        pdfList.add(PdfDocument.fromFile(Paths.get("assets/2.pdf")));
        pdfList.add(PdfDocument.fromFile(Paths.get("assets/3.pdf")));
        // Merge all PDF documents in the list into one
        PdfDocument merged = PdfDocument.merge(pdfList);
        // Save the merged PDF document to the specified path
        merged.saveAs(Paths.get("assets/merged.pdf"));
    }
}
JAVA

結論

本文涵蓋如何使用Java和IronPDF程式庫合併多個PDF文件。 按照本文中的步驟,您將能夠設置環境、導入程式庫、讀取輸入文件並將其合併成單個文件。

如需有關在Java中使用IronPDF合併PDF文件和類似的如何從HTML創建PDF使用IronPDF格式化PDF的教程,請探索我們的全面檔

IronPDF for Java可供開發用途免費使用,但在生產環境中使用需要商業授權

常見問題解答

如何使用Java合併兩個PDF檔案?

您可以使用IronPDF for Java 中的PdfDocument類別來合併兩個 PDF 檔案。首先,使用PdfDocument.fromFile方法載入 PDF 文檔,然後使用merge方法將它們合併成一個文檔,最後使用saveAs儲存輸出結果。

如何設定IronPDF for Java?

若要為 Java 設定IronPDF ,請確保已安裝 Java 和 Java IDE(例如 IntelliJ)。透過從IronPDF網站或 Maven Central 取得必要的依賴項,將IronPDF新增為專案中的 Maven 依賴項。

IronPDF for Java 能否合併兩個以上的 PDF 檔?

是的, IronPDF for Java 可以合併兩個以上的 PDF 檔案。您可以將多個 PDF 文件載入到一個清單中,然後使用merge方法將它們合併成一個 PDF 文件。

如何在Java中從HTML建立PDF文件?

IronPDF for Java 可讓您使用HtmlToPdf.renderHtmlAsPdf方法從 HTML 建立 PDF 文件。您可以直接將 HTML 字串或檔案渲染成 PDF。

IronPDF for Java 是否適用於生產環境?

IronPDF for Java 可免費用於開發目的,但在生產環境中部署則需要商業許可證。

在 Java 中使用IronPDF需要哪些前提條件?

前提條件包括安裝和設定 Java,使用 Java IDE(例如 IntelliJ IDEA),以及將IronPDF作為 Maven 依賴項整合到您的專案中。

哪裡可以找到IronPDF for Java 的文檔?

IronPDF官方網站提供了IronPDF for Java 的完整文檔,包括合併 PDF 和從 HTML 建立 PDF 的指南。

如何排查在 Java 中合併 PDF 時所出現的問題?

請確保使用 IronPDF 的PdfDocument.fromFile方法正確載入所有 PDF 文件,並驗證IronPDF庫是否已正確新增為 Maven 依賴項。更多故障排除提示,請參閱IronPDF文件。

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

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

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

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

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me