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 分钟前
《github star 加星 Taimili.com 艾米莉 》为什么Java里面,Service 层不直接返回 Result 对象?
java·后端·github
Maori3161 小时前
放弃 SDKMAN!在 Garuda Linux + Fish 环境下的优雅 Java 管理指南
java
用户908324602731 小时前
Spring AI 1.1.2 + Neo4j:用知识图谱增强 RAG 检索(上篇:图谱构建)
java·spring boot
小王和八蛋2 小时前
DecimalFormat 与 BigDecimal
java·后端
beata2 小时前
Java基础-16:Java内置锁的四种状态及其转换机制详解-从无锁到重量级锁的进化与优化指南
java·后端
IT探险家2 小时前
你的第一个 Java 程序就翻车?HelloWorld 的 8 个隐藏陷阱
java
随风飘的云2 小时前
SpringBoot 的自动配置原理
java
SimonKing2 小时前
觅得又一款轻量级数据库管理工具:GoNavi
java·后端·程序员
Seven973 小时前
BIO详解:解锁阻塞IO的使用方式
java
oak隔壁找我13 小时前
JVM常用调优参数
java·后端