java对pdf文件分页拆分

文章目录

pdf文件拆分

  1. 导入依赖
xml 复制代码
<dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.32</version>
        </dependency>
java 复制代码
2. 大文件拆分
 public static boolean splitPdf() {
        try {
            long st = System.currentTimeMillis();
            log.info("文件拆分进度 start");
            File inputFile = new File("D:\\pdftest\\bigpdf.pdf"); // 输入的PDF文件路径
            PDDocument document = PDDocument.load(inputFile);
            int numberOfPages = document.getNumberOfPages();
            for (int i = 0; i < numberOfPages; i++) {
                log.info("文件拆分进度 " + (i + 1) + "/" + numberOfPages);
                PDDocument splitDocument = new PDDocument();
                PDPage page = document.getPage(i);
                splitDocument.addPage(page);
                String outputFilePath = "D:\\pdftest\\test\\output_" + (i + 1) + ".pdf"; // 输出文件的路径和名称
                splitDocument.save(outputFilePath);
                splitDocument.close();
            }
            document.close();
            log.info("文件拆分进度 end");
            long end = System.currentTimeMillis();
            log.info("拆分和检测总耗时:" + (end - st) + " ms");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

指定分页大小

可以根据需要,决定将哪些分页放在一个文件里面。

相关推荐
geovindu1 小时前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式
skywalk81632 小时前
把「假成功」赶出工具层:lightharness 两天升级复盘(4×5 模式矩阵 + 工具层 5 个缺陷)
开发语言·前端·人工智能·lightharness
敲上瘾2 小时前
Qt控件通用属性:geometry、qrc资源到QSS样式表等
开发语言·c++·qt·ui
听情歌落俗4 小时前
嵌入式学习1-C语言基础语法与STM32实践
c语言·开发语言
小飞在努力4 小时前
【实现负载均匀在线判题⑤】MVC 架构搭建与 Model 题目数据模块实现
c语言·开发语言·c++·笔记·后端
落羽的落羽5 小时前
【C++】现代容器: tuple、optional、variant、any、string_view
linux·开发语言·数据结构·c++·人工智能·计算机网络·算法
会编程的吕洞宾6 小时前
Spring AI 2.0 工具调用实战:让 AI 自动调用你的 Java 方法
java·人工智能·经验分享·后端·spring
斯维赤6 小时前
SELECT * 为什么是覆盖索引的天敌?
java·后端·mysql
stormsha6 小时前
库存 5,10 个并发各买 1 件:为什么最后不剩 -5 件
java·数据库·mysql
小飞在努力7 小时前
【实现负载均匀在线判题④】接入 cpp-httplib 实现编译服务 HTTP 网络化
c语言·开发语言·c++·笔记·http