跳至頁尾內容
USING 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. 應安裝Eclipse或IntelliJ等Java IDE。 您可以從這裡的官方Eclipse下載頁面下載Eclipse,並從JetBrains的下載部分下載IntelliJ。
  3. 應下載IronPDF library for Java並在您的專案中新增為依賴項。 您可以在IronPDF官方網站學習如何執行此操作。
  4. 在開始PDF轉換之前,應安裝Maven並將其整合到您的IDE中。 關於安裝Maven及其整合環境的教程,請存取JetBrains的逐步Maven教程

IronPDF for Java安裝

如果所有要求都已滿足,那麼即使對於Java新手來說,安裝IronPDF for Java也是非常簡單明瞭的。

在本文中,將使用JetBrains的IntelliJ IDEA來安裝和運行範例。

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

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

一個新窗口將出現。 輸入專案名稱並點擊完成。

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

點擊完成後,將打開一個新項目到pom.xml文件中,以新增Java版IronPDF的Maven依賴項。

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

pom.xml文件中新增以下依賴項,或者您可以從Maven中央庫的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文件

您可以使用Java版IronPDF輕鬆地合併多於兩個的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 library來合併多個PDF文件。 按照本文中描述的步驟,您將可以設置環境、匯入程式庫、讀取輸入文件並將它們合併成單一文件。

如需有關使用IronPDF在Java中合併PDF文件的更多資訊,及有關從HTML建立PDF文件使用IronPDF格式化PDF文件的相關教程,請查閱我們的詳細文件

IronPDF for Java在開發目的下免費使用,但在生產環境中使用需要商業授權

常見問題

如何使用Java合併兩個PDF文件?

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

設置IronPDF for Java的步驟是什麼?

要設置IronPDF for Java,確保已安裝Java和像IntelliJ這樣的Java IDE。在您的專案中新增IronPDF作為Maven依賴,包括從IronPDF網站或Maven Central的必要依賴項。

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可免費用於開發目的,但在生產環境中部署需要商業授權。

使用IronPDF in Java的先決條件是什麼?

先決條件包括已安裝並配置Java,使用像IntelliJ IDEA這樣的Java IDE,並在您的專案中整合IronPDF作為Maven依賴。

我可以在哪裡找到IronPDF for Java的文件?

IronPDF for Java的綜合文件,包括合併PDF和從HTML建立PDF的指南,可以在IronPDF官方網站上找到。

如何在Java中合併PDF時進行故障排除?

確保所有PDF文件均正確載入,使用IronPDF的PdfDocument.fromFile方法,並驗證IronPDF程式庫已正確新增為Maven依賴。檢查IronPDF文件以獲取額外的故障排除提示。

Darrius Serrant
全端軟體工程師(WebOps)

Darrius Serrant擁有邁阿密大學的電腦科學學士學位,並在Iron Software擔任全端WebOps行銷工程師。從小就對程式設計有興趣,他認為計算既神秘又易於理解,成為創意和問題解決的完美媒介。

在Iron Software,Darrius喜歡創造新事物並簡化複雜的概念,使其更易於理解。作為我們的常駐開發人員之一,他還志願教學,將他的專業知識傳授給下一代。

對Darrius來說,他的工作是有意義的,因為它有價值且對社會有真正的影響。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話