Java实现pdf文件合并的使用示例

 更新时间:2023年12月15日 09:39:08   作者:lang20150928  
本文主要介绍了Java实现pdf文件合并的使用示例,主要是将需要合并的pdf文件都拷贝到指定目录a中,调用该工具类将该目录作为第一个参数,第二个参数传入输出文件对象即可,感兴趣的可以了解一下

在maven项目中引入以下依赖包

    <dependencies>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox-examples</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.9.0</version>
        </dependency>
    </dependencies>

创建一个工具类

package org.apache.pdfbox.utils;

import org.apache.commons.io.FileUtils;
import org.apache.pdfbox.examples.util.PDFMergerExample;
import org.apache.pdfbox.io.RandomAccessRead;
import org.apache.pdfbox.io.RandomAccessReadMemoryMappedFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author: guanglai.zhou
 * @date: 2023/12/14 13:15
 */
public class PdfMergerUtils {

    /**
     * 合并指定目录中的pdf文件
     *
     * @param fromDir  指定目录
     * @param descFile 目标pdf文件
     * @return 目标pdf文件
     * @throws IOException
     */
    public static File merge(String fromDir, String descFile) throws IOException {
        final File resultFile = new File(descFile);
        File file = new File(fromDir);
        List<File> files = new ArrayList<>();
        list(file, new Predicate<File>() {
            @Override
            public boolean test(File file) {
                return true;
            }
        }, new Predicate<File>() {
            // 选择pdf文件
            @Override
            public boolean test(File file) {
                return file.getPath().endsWith(".pdf");
            }
        }, files);
        if (files.isEmpty()) {
            throw new RuntimeException("源文件不存在pdf格式文档?");
        }
//        files.sort(Comparator.comparing(File::getName));
        if (resultFile.exists()) {
            FileUtils.forceDelete(resultFile);
        }
        mergePdfs(resultFile, files);
        return resultFile;
    }

    /**
     * 针对文件进行遍历 如果文件夹满足directoryPredicate,则继续遍历文件夹,如果是文件,则判断是否满足filePredicate,如果满足则添加到
     * collector结果集当中
     *
     * @param file               文件夹
     * @param directoryPredicate 文件夹预期 为null 则不针对文件夹做过滤
     * @param filePredicate      文件预期 为null 则不针对文件做过滤
     * @param collector          收集器 收集所有符合条件的文件
     */
    public static void list(File file, Predicate<File> directoryPredicate, Predicate<File> filePredicate, List<File> collector) {
        File[] childFiles = file.listFiles();
        if (childFiles == null) {
            return;
        }
        // 根据脚本名称进行排序
        List<File> fileList = Arrays.stream(childFiles).sorted(Comparator.comparing(File::getName)).collect(Collectors.toList());
        for (File childFile : fileList) {
            if (childFile.isDirectory()) {
                boolean pass = directoryPredicate == null || directoryPredicate.test(childFile);
                if (pass) {
                    // 继续遍历子文件夹目录
                    list(childFile, directoryPredicate, filePredicate, collector);
                }
            } else {
                boolean pass = filePredicate == null || filePredicate.test(childFile);
                if (pass) {
                    collector.add(childFile);
                }
            }
        }
    }

    private static void mergePdfs(File resultFile, List<File> files) throws IOException {
        PDFMergerExample example = new PDFMergerExample();
        List<RandomAccessRead> sources = new ArrayList<>();
        for (File currFile : files) {
            sources.add(new RandomAccessReadMemoryMappedFile(currFile));
        }
        InputStream inputStream = example.merge(sources);
        FileUtils.copyInputStreamToFile(inputStream, resultFile);
    }

}

将需要合并的pdf文件都拷贝到指定目录a中,调用该工具类将该目录作为第一个参数,第二个参数传入输出文件对象即可。

到此这篇关于Java实现pdf文件合并的使用示例的文章就介绍到这了,更多相关Java pdf文件合并内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • SpringBoot项目使用jasypt加解密的方法

    SpringBoot项目使用jasypt加解密的方法

    jasypt是一个通用的加解密库,我们可以使用它在配置文件中对数据库密码进行加密,以确保其安全性,接下来通过本文给大家介绍SpringBoot项目使用jasypt加解密的方法,感兴趣的朋友一起看看吧
    2022-05-05
  • spring boot+自定义 AOP 实现全局校验的实例代码

    spring boot+自定义 AOP 实现全局校验的实例代码

    最近公司重构项目,重构为最热的微服务框架 spring boot, 重构的时候遇到几个可以统一处理的问题。这篇文章主要介绍了spring boot+自定义 AOP 实现全局校验 ,需要的朋友可以参考下
    2019-04-04
  • Springboot过滤器禁止ip频繁访问功能实现

    Springboot过滤器禁止ip频繁访问功能实现

    这篇文章主要介绍了Springboot过滤器禁止ip频繁访问功能实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • SpringBoot如何返回Json数据格式

    SpringBoot如何返回Json数据格式

    这篇文章主要介绍了SpringBoot如何返回Json数据格式问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • 如何利用 Either 和 Option 进行函数式错误处理

    如何利用 Either 和 Option 进行函数式错误处理

    这篇文章主要介绍了如何利用 Either 和 Option 进行函数式错误处理。在 Java 中,错误的处理在传统上由异常以及创建和传播异常的语言支持进行。但是,如果不存在结构化异常处理又如何呢?,需要的朋友可以参考下
    2019-06-06
  • 一篇文章带了解如何用SpringBoot在RequestBody中优雅的使用枚举参数

    一篇文章带了解如何用SpringBoot在RequestBody中优雅的使用枚举参数

    这篇文章主要介绍了SpringBoot中RequestBodyAdvice使用枚举参数,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • mybatis resultmap 如何为对象赋值的调用顺序

    mybatis resultmap 如何为对象赋值的调用顺序

    这篇文章主要介绍了mybatis resultmap 如何为对象赋值的调用顺序,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • springboot中Excel文件下载踩坑大全

    springboot中Excel文件下载踩坑大全

    本文主要介绍了springboot中Excel文件下载,但是却容易遇到很多坑,文中通过示例代码介绍的非常详细,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • 一文教会你用mybatis查询数据库数据

    一文教会你用mybatis查询数据库数据

    MyBatis本身是一个数据库连接框架,可以认为是JDBC的升级版,下面这篇文章主要给大家介绍了关于mybatis查询数据库数据的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2022-04-04
  • Springboot项目长时间不进行接口操作,提示HikariPool-1警告的解决

    Springboot项目长时间不进行接口操作,提示HikariPool-1警告的解决

    这篇文章主要介绍了Springboot项目长时间不进行接口操作,提示HikariPool-1警告的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12

最新评论