pdf多文件合并

【第三方工具】点我传送https://www.ilovepdf.com/

【java功能实现】

导入jar包

javascript 复制代码
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13.4</version>
        </dependency>

代码

javascript 复制代码
package com.czh.pdf_spring;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfReader;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.*;

@SpringBootTest
class PdfSpringApplicationTests {
    /**
     * 指定文件夹
     * @throws FileNotFoundException
     */
    @Test
    void test() throws IOException, DocumentException {
        //pdf文件夹
        String path = "D:/pdf";
        //绝对路径
        String outputPdf = "D:/test_pdf.pdf";
        //获取文件夹下的所有文件
        File[] files = new File(path).listFiles();
        if(files != null){
            //创建文档
            Document document = new Document();
            //文档名称
            PdfCopy copy = new PdfCopy(document, new FileOutputStream(outputPdf));
            //打开文档
            document.open();
            //写入类容
            for (File file : files) {
                //创建输入流
                FileInputStream inputStream = new FileInputStream(file);
                PdfReader reader = new PdfReader(inputStream);
                    for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                        document.newPage();
                        copy.addPage(copy.getImportedPage(reader, i));
                    }
                reader.close();
                inputStream.close();
            }
            //关闭文档
            document.close();
        }
        System.out.println("pdf file successfully!");
    }
}

由于朋友工作需要,所以帮了下忙,链接作者是指定的文件名,朋友的pdf文件巨多,不方便,所以改成了指定文件夹的方式,

参考链接:https://blog.csdn.net/yuchenff/article/details/143170405

相关推荐
超级大只老咪4 小时前
数组相邻元素比较的循环条件(Java竞赛考点)
java
小浣熊熊熊熊熊熊熊丶4 小时前
《Effective Java》第25条:限制源文件为单个顶级类
java·开发语言·effective java
毕设源码-钟学长5 小时前
【开题答辩全过程】以 公交管理系统为例,包含答辩的问题和答案
java·eclipse
啃火龙果的兔子5 小时前
JDK 安装配置
java·开发语言
星哥说事5 小时前
应用程序监控:Java 与 Web 应用的实践
java·开发语言
派大鑫wink5 小时前
【JAVA学习日志】SpringBoot 参数配置:从基础到实战,解锁灵活配置新姿势
java·spring boot·后端
xUxIAOrUIII5 小时前
【Spring Boot】控制器Controller方法
java·spring boot·后端
Dolphin_Home5 小时前
从理论到实战:图结构在仓库关联业务中的落地(小白→中级,附完整代码)
java·spring boot·后端·spring cloud·database·广度优先·图搜索算法
醇氧5 小时前
org.jetbrains.annotations的@Nullable 学习
java·开发语言·学习·intellij-idea
Java&Develop5 小时前
Aes加密 GCM java
java·开发语言·python