当前位置:网站首页>使用itextpdf实现截取pdf文档第几页到第几页,进行分片
使用itextpdf实现截取pdf文档第几页到第几页,进行分片
2022-04-23 14:03:00 【洋哥登陆】
itextpdf实现截取pdf文档第几页到第几页
起因:pdf文档太大,100M以上,导致前端根本无法显示。解决方案,后端进行pdf文档分片操作,例如十页十页的传给前端。
1.使用itextpdf
导入依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
2.业务逻辑
@GetMapping("/download")
public void pdf(@RequestParam(value = "page", required = false) String page, HttpServletResponse response) throws IOException, DocumentException {
//文件路径
path = "C:\\Users\\pdf文件.pdf"";
// 读取pdf文档
PdfReader reader = new PdfReader(path);
//总页数
int numberOfPages = reader.getNumberOfPages();
// 截取开始页
int start = Integer.parseInt(page.substring(0, 1));
//截取pdf部分页,格式"2-5" 第2页到第5页 页码超出范围(10页,你选择"15-20")只会读最后一页
// 参数为String型,可让前端传值,控制读取第几页
// reader.selectPages("2-5");
reader.selectPages(page);
//源码没怎么看懂,但是需要内存中存放文件流,所以用了HttpServletResponse
PdfStamper stamp = new PdfStamper(reader, response.getOutputStream());
// 开始页 如果大于pdf总页数,不返回文件流,stamp.close()结果返回1
if(start <= numberOfPages){
stamp.close();
}
reader.close();
}
3.结果
一千多页的文档,我选择截取1-5页,那么只会显示1,2,3,4,5这几页
版权声明
本文为[洋哥登陆]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Peanutfight/article/details/113853241
边栏推荐
猜你喜欢
RobotFramework 之 用例标签机制
联想产品经理林林:天津当地网络运营商网络故障 ZUI系统后台服务器暂时无法正常工作
Mock测试
How does redis solve the problems of cache avalanche, cache breakdown and cache penetration
Lin Lin, product manager of Lenovo: network failure of local network operator in Tianjin. The background server of Zui system can't work normally for the time being
Quartus Prime硬件实验开发(DE2-115板)实验一CPU指令运算器设计
nodejs通过require读取本地json文件出现Unexpected token / in JSON at position
Nacos+AspnetCore+Ocelot实战编码
室内外地图切换(室内基于ibeacons三点定位)
PATH环境变量
随机推荐
request模块
JS 力扣刷题 102. 二叉树的层序遍历
读了一篇博客,重新理解闭包整理一下
Quartus Prime硬件实验开发(DE2-115板)实验一CPU指令运算器设计
go 语言 数组,字符串,切片
变长参数__VA_ARGS__ 和 写日志的宏定义
AtomicIntegerArray源码分析与感悟
SQL数据库
Neuron and neural network
多重继承虚基类习题
Prediction of tomorrow's trading limit of Low Frequency Quantization
村上春树 --《当我谈跑步时,我谈些什么》句子摘录
JS 力扣刷题 103. 二叉树的锯齿形层序遍历
BUG_me
Business case | how to promote the activity of sports and health app users? It is enough to do these points well
关于stream流,浅记一下------
Nacos+AspnetCore+Ocelot实战编码
Node接入支付宝开放平台的沙箱实现支付功能
Strange bug of cnpm
Special test 05 · double integral [Li Yanfang's whole class]