当前位置:网站首页>使用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
边栏推荐
- As a junior college student, I studied hard in closed doors for 56 days, won Ali offer with tears, five rounds of interviews and six hours of soul torture
- SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
- Scientists say Australian plan to cull up to 10,000 wild horses doesn’t go far enough
- pthread_self()为何重复了
- Oracle告警日志alert.log和跟踪trace文件中文乱码显示
- Quartus Prime硬件实验开发(DE2-115板)实验一CPU指令运算器设计
- Introduction to spark basic operation
- leetcode--357. Count the number of different figures
- Jacob print word
- 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
猜你喜欢
Crontab timing task output generates a large number of mail and runs out of file system inode problem processing
修改ddt生成的测试用例名称
编程旅行之函数
go 语言 数组,字符串,切片
Node接入支付宝开放平台的沙箱实现支付功能
Promtail + Loki + Grafana 日志监控系统搭建
Qt Designer怎样加入资源文件
CDH cluster integration Phoenix based on CM management
Choreographer full resolution
Kettle -- control parsing
随机推荐
微信小程序调用客服接口
Basic knowledge learning record
快捷键(多行)
金蝶云星空API调用实践
freeCodeCamp----time_ Calculator exercise
L2-024 tribe (25 points)
Jacob print word
freeCodeCamp----arithmetic_ Arranger exercise
Pytorch 经典卷积神经网络 LeNet
Go语言 RPC通讯
request模块
Program compilation and debugging learning record
Ptorch classical convolutional neural network lenet
json反序列化匿名数组/对象
关于pthread多线程一些好文章
Neuron and neural network
China creates vast research infrastructure to support ambitious climate goals
The latest development of fed digital currency
How does redis solve the problems of cache avalanche, cache breakdown and cache penetration
基于Ocelot的gRpc网关