Java 中将多个 PDF 文件合并为一个 PDF

一.前言

我们将从以下两个方面向您展示如何将多个PDF文件合并为一个PDF:
1. 将文件中的多个 PDF 合并为单个 PDF
2. 将流中的多个 PDF 合并为单个 PDF

1. 了解 Spire.PDF 库

要在 Java 中合并 PDF 文件,我们将使用Spire.PDF 库。Spire.PDF for Java 是一个 PDF API,使 Java 应用程序能够在不使用 Adob​​e Acrobat 的情况下读取、写入和保存 PDF 文档。它提供了用于操作 PDF 文件的广泛功能,包括将多个 PDF 文件合并到单个文档中。

在我们使用 Spire.PDF 合并 PDF 文件之前,我们需要将其依赖项添加到我们的 Java 项目中。我们可以通过向 Maven 项目添加以下依赖项来实现此目的:

xml 复制代码
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>9.5.6</version>
    </dependency>
</dependencies>

2.将文件中的多个 PDF 合并为单个 PDF

  1. 获取要合并的文档的路径并将其存储在 String 数组中。
  2. 使用PdfDocument.mergeFiles()方法合并选定的 PDF 文件。
  3. 使用PdfDocumentBase.save()方法保存 PDF 文档。
java 复制代码
import com.spire.pdf.*;

public class mergePDF {

    public static void main(String[] args) throws Exception {

        //Get the paths of the documents to be merged
        String[] files = new String[] {
                "D:\\sample.pdf",
                "D:\\sample1.pdf"};

        //Merge documents and return an object of PdfDocumentBase
        PdfDocumentBase pdf = PdfDocument.mergeFiles(files);

        //Save the result to a PDF file
        pdf.save("MergedPDF.pdf", FileFormat.PDF);

    }
}

3.将流中的多个 PDF 合并为单个 PDF

从流中加载 PDF 并将其合并为新的 PDF 是处理多个 PDF 文档而无需将它们保存到磁盘的便捷有效的方法。当处理您不想存储在本地驱动器上的大型或敏感文件时,此方法特别有用。Spire.PDF还支持从流中加载PDF,然后将它们组合成一个新的PDF文件。

  1. 获取 PDF 流的路径,然后将它们存储到FileInputStream数组中。
  2. 使用PdfDocument.mergeFiles()方法合并选定的 PDF 文件。
  3. 使用PdfDocumentBase.save()方法保存 PDF 文档。
java 复制代码
import com.spire.pdf.*;
import java.io.*;

public class mergePDFbyStream {

    public static void main(String[] args) throws Exception {

        FileInputStream stream1 = new FileInputStream(new File("sample.pdf"));
        FileInputStream stream2 = new FileInputStream(new File("sample1.pdf"));

        InputStream[] streams = new FileInputStream[]{stream1, stream2};

        //Merge these documents and return an object of PdfDocumentBase
        PdfDocumentBase pdf = PdfDocument.mergeFiles(streams);

        //Save the result to a PDF file
        pdf.save("MergedPDF.pdf", FileFormat.PDF);

    }
}
相关推荐
用户03321266636718 小时前
使用 Python 从零创建 Word 文档
python
程序员晓琪18 小时前
约定大于配置:基于 Java 包名自动生成 API 版本路由的最佳实践
java·spring boot·后端
Flittly18 小时前
【AgentScope Java新手村系列】(11)中断与恢复
java·spring boot·spring
众少成多积小致巨19 小时前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
东坡白菜19 小时前
破局全栈:前端开发的Java入门实战记录—JPA(2)
java·后端
Csvn1 天前
Python 两大经典坑点 —— 可变默认参数 & 闭包延迟绑定
后端·python
曲幽1 天前
别再用网页翻译看源码了!你的私人翻译神器LibreTranslate,部署避坑指南来了
python·docker·web·pot·translate·libretranslate·arogstranslate
SimonKing1 天前
艹,维护AI写的代码,我心态崩了......
java·后端·程序员
用户556918817531 天前
#从脚本到独立程序:Python + Playwright 批量抓取的完整踩坑记录
python·自动化运维
用户298698530141 天前
Java Word 文档样式进阶:段落与文本背景色设置完全指南
java·后端