如何在 Java 中合併兩個 PDF 文件
本文將示範如何使用Java 版 IronPDF 庫合併多個 PDF 文件。 我們將逐步完成環境設定、庫導入、讀取輸入檔案以及將它們合併成一個文件的過程。
如何在 Java 中合併兩個 PDF 文件
- 安裝 Java 程式庫以執行 PDF 合併
- 導入或渲染 PDF 以進行合併
- 使用 PdfDocument 類合併 PDF
- 將合併的 PDF 導出為單個文件
- 檢查生成的 PDF
IronPDF for Java
IronPDF for Java 是一個功能強大的程式庫,它允許開發人員從頭開始建立新的 PDF 文檔,並將各種文件格式轉換為 PDF 文件。 它還提供了將多個 PDF 文件合併成一個文件的功能。
IronPDF for Java 易於使用,並具有簡單直覺的 API,可讓開發人員輕鬆建立 PDF 檔案。 它還支援在 PDF 中渲染圖表、處理PDF 表單,甚至以程式設計方式處理數位簽章的方法。
先決條件
在實作之前,必須滿足一些先決條件才能執行 PDF 建立流程。
- 您的系統應該安裝 Java,並且應該在環境變數中設定其路徑。 如果您尚未安裝 Java,請參考Java 網站上的安裝指南以取得說明。
- 需要安裝 Java IDE,例如 Eclipse 或 IntelliJ。 您可以從Eclipse 官方下載頁面下載 Eclipse,從JetBrains 下載頁面下載 IntelliJ。
- 需要下載 IronPDF Java 庫並將其作為依賴項新增至您的專案。 您可以在IronPDF 官方網站上學習如何操作。
- 在開始 PDF 轉換之前,應先安裝 Maven 並將其整合到您的 IDE 中。 有關安裝 Maven 並將其整合到您的環境中的教程,請造訪JetBrains 提供的 Maven 逐步教學。
IronPDF 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 文件,新增 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>在pom.xml檔案中新增依賴項後,檔案右上角會出現一個小圖示。
如何使用 Java 合併兩個 PDF 文件,圖 4:按一下浮動圖示自動安裝 Maven 依賴項 點擊懸浮圖示即可自動安裝 Maven 依賴項。
點擊此圖示安裝 IronPDF for Java 的 Maven 依賴項。 這只需要幾分鐘,具體時間取決於您的網路連線速度。
合併多個PDF文檔
IronPDF 讓您可以使用 Java 程式將多個 PDF 文件合併為一個 PDF 文件。 IronPDF 提供了多種合併 PDF 文件的方法:
- 建立兩個新的 PDF 文檔,並將它們合併為一個 PDF 文檔。
- 將輸入的 PDF 檔案合併成一個 PDF 檔案。
- 合併兩個以上的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 合併兩個 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 合併兩個 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 和 IronPDF 庫合併多個 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 和 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、使用 IntelliJ IDEA 等 Java IDE,以及在專案中將 IronPDF 整合為 Maven 的相依性。
在哪裡可以找到 IronPDF for Java 的說明文件?
IronPDF for Java 的全面說明文件,包括合併 PDF 和從 HTML 創建 PDF 的指南,可在 IronPDF 官方網站找到。
在 Java 中合併 PDF 時,如何排除故障?
確保使用 IronPDF 的 PdfDocument.fromFile 方法正確載入所有 PDF 檔案,並驗證 IronPDF 函式庫是否已正確加入為 Maven 的相依性。檢視 IronPDF 文件以取得其他疑難排解提示。







