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);

    }
}
相关推荐
杨杨杨大侠4 小时前
Jev、Kev、Laya:决策模型怎么选,什么时候需要微调?
人工智能·python·agent
EatFan4 小时前
Spring Boot 4 落地观察:从 yudao-cloud、matecloud、JPower 看国产脚手架的升级路线与迁移清单
java·spring boot·后端·spring cloud·微服务·后端开发·jdk 21
LOVE️YOU5 小时前
Python 在定义函数时,究竟定义的是什么?
python
Maiko Star5 小时前
* LangChain 提示词模板详解:ChatPromptTemplate 的使用与高级特性
java·人工智能·langchain
段一凡-华北理工大学5 小时前
大模型应用开发 100 天:Python + LLM 从入门到精通 day26~Prompt 调试与优化——A/B 测试与效果评估
windows·python·大模型·prompt·智能体·提示词工程·高炉智能化
Minecraft红客5 小时前
以撒的结合
python·游戏·电脑·娱乐
Circ.5 小时前
Python 调试 Litellm 本地大模型接口:模型列表与对话接口实操踩坑记录
开发语言·python
风早爽太6 小时前
用 ‌Render‌ 快速部署网站和常见问题解决
python·fastapi
现任明教教主~7 小时前
Thinkphp站群蜘蛛池SaaS系统YanyvSEO含多用户/积分/六大引擎计费/易支付对接
java·开发语言·spring
李航19837 小时前
给自己的图形引擎,配上了AI渲染,做设计真是太方便了
人工智能·python·计算机视觉·ai·ai编程