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;
    }

指定分页大小

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

相关推荐
典孝赢麻崩乐急7 分钟前
Java学习-------外观模式
java·学习·外观模式
MacroZheng14 分钟前
换掉Navicat!一款集成AI功能的数据库管理工具,功能真心强大!
java·后端·mysql
橘子在努力17 分钟前
【橘子分布式】gRPC(番外篇-客户端重试机制)
java·分布式·rpc
五行缺弦30 分钟前
Java 笔记 serialVersionUID
java·开发语言·笔记
neoooo39 分钟前
《锁得住,才能活得久》——一篇讲透 Redisson 分布式锁的技术实录
java·spring boot·redis
花落人散处1 小时前
SpringAI——整合MCP案例
java·后端
打码农的篮球1 小时前
C++模板
开发语言·c++
小乖兽技术1 小时前
在 .NET 中使用 Base64 时容易踩的坑总结
开发语言·c#·.net
胡斌附体1 小时前
mybatis-plus逻辑删除配置
java·mybatis·mybatis-plus·逻辑删除
Lenyiin2 小时前
《LeetCode 热题 100》整整 100 题量大管饱题解套餐 中
java·c++·python·leetcode·面试·刷题·lenyiin