如何在 Java 中合併兩個 PDF 文件
本文將示範如何使用IronPDF Library for Java合併多個PDF文件。 我們將探討設定環境、匯入程式庫、讀取輸入檔案,並將它們合併成單一文件的過程。
使用Java合併兩個PDF文件的方法
- 安裝Java Library以執行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網站上的安裝指南。
- 應安裝Eclipse或IntelliJ等Java IDE。 您可以從這裡的官方Eclipse下載頁面下載Eclipse,並從JetBrains的下載部分下載IntelliJ。
- 應下載IronPDF library for Java並在您的專案中新增為依賴項。 您可以在IronPDF官方網站學習如何執行此操作。
- 在開始PDF轉換之前,應安裝Maven並將其整合到您的IDE中。 關於安裝Maven及其整合環境的教程,請存取JetBrains的逐步Maven教程。
IronPDF for Java安裝
如果所有要求都已滿足,那麼即使對於Java新手來說,安裝IronPDF for Java也是非常簡單明瞭的。
在本文中,將使用JetBrains的IntelliJ IDEA來安裝和運行範例。
首先,打開JetBrains IntelliJ IDEA並建立一個新的Maven專案。
在 IntelliJ 中建立新的 Maven 專案
一個新窗口將出現。 輸入專案名稱並點擊完成。
給 Maven 專案命名並點擊完成
點擊完成後,將打開一個新項目到pom.xml文件中,以新增Java版IronPDF的Maven依賴項。
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>
一旦您在pom.xml文件中放置了依賴項,該文件的右上角將出現一個小圖標。
點擊浮動圖標以自動安裝 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"));
}
}
新的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"));
}
}
現有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和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文件以獲取額外的故障排除提示。





