pdf多文件合并

【第三方工具】点我传送:https://www.ilovepdf.com/
【java功能实现】
导入jar包

<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13.4</version>
        </dependency>

代码

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值