為了合併 PDF 文件,我們將使用 Aspose.PDF for Java API,這是一個功能豐富、強大且易於使用的 Java 平台文件合併 API。我們的 Java 程式庫可以將任何受支援的下載格式的文件轉換為任何受支援的儲存格式。 Aspose.PDF for Java 程式庫提供了相當通用的解決方案,可以幫助您完成文件轉換任務。 Aspose.PDF 支援最多的常用文件格式,包括載入和儲存。請注意,本節僅介紹常用的轉換。本頁提供有關格式轉換的資訊。但是,文件轉換的組合方式有很多種。有關支援格式的完整列表,請參閱 支援的文件格式 部分。您可以直接從 Maven 下載其最新版本,並透過將下列設定新增至 pom.xml 中將其安裝至基於 Maven 的專案中。
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java AP</name>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>
通過Java合併 PDF 檔
您需要 Aspose.PDF for Java 才能在您的環境中測試程式碼。
- 開啟第一個文件。
- 開啟第二個文件。
- 將第二個文件的頁面添加到第一個文件。
- 儲存串聯的輸出檔
Java-合并 PDF 文件的示例代码
// Open first document
Document pdfDocument1 = new Document(DATA_DIR + "Concat1.pdf");
// Open second document
Document pdfDocument2 = new Document(DATA_DIR + "Concat2.pdf");
// Add pages of second document to the first
pdfDocument1.merge(pdfDocument2);
// Save concatenated output file
pdfDocument1.save(DATA_DIR + "ConcatenatePdfFiles_out.pdf");
// Close documents
pdfDocument1.close();