当前位置:网站首页>Use itextpdf to intercept the page to page of PDF document and divide it into pieces
Use itextpdf to intercept the page to page of PDF document and divide it into pieces
2022-04-23 16:31:00 【Yangge landing】
itextpdf Implement interception pdf From page to page of the document
cause :pdf Document too large ,100M above , The front end cannot be displayed at all . Solution , Back end pdf Document slicing operation , For example, ten pages are sent to the front end .
1. Use itextpdf
Import dependence
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
2. Business logic
@GetMapping("/download")
public void pdf(@RequestParam(value = "page", required = false) String page, HttpServletResponse response) throws IOException, DocumentException {
// File path
path = "C:\\Users\\pdf file .pdf"";
// Read pdf file
PdfReader reader = new PdfReader(path);
// Total number of pages
int numberOfPages = reader.getNumberOfPages();
// Intercept the start page
int start = Integer.parseInt(page.substring(0, 1));
// Intercept pdf Partial page , Format "2-5" The first 2 To the first page 5 page Page number out of range (10 page , You choose "15-20") Can only read the last page
// Parameter is String type , Allow the front end to transmit value , Control which page to read
// reader.selectPages("2-5");
reader.selectPages(page);
// I don't understand the source code very much , But you need to store the file stream in memory , So use the HttpServletResponse
PdfStamper stamp = new PdfStamper(reader, response.getOutputStream());
// Start page If it is greater than pdf Total number of pages , Do not return file stream ,stamp.close() The result returned to 1
if(start <= numberOfPages){
stamp.close();
}
reader.close();
}
3. result
More than a thousand pages of documents , I choose to intercept 1-5 page , Then it will only show 1,2,3,4,5 These pages
版权声明
本文为[Yangge landing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231402542936.html
边栏推荐
- Introduction notes to PHP zero Foundation (13): array related functions
- JSP learning 1
- Force buckle-746 Climb stairs with minimum cost
- JMeter setting environment variable supports direct startup by entering JMeter in any terminal directory
- Oak-d raspberry pie cloud project [with detailed code]
- RAID磁盘阵列与RAID5的创建
- Detailed explanation of gzip and gunzip decompression parameters
- 04 Lua 运算符
- VMware Workstation cannot connect to the virtual machine. The system cannot find the specified file
- linux上啟動oracle服務
猜你喜欢
ESP32编译环境的搭建
The first line and the last two lines are frozen when paging
Take according to the actual situation, classify and summarize once every three levels, and see the figure to know the demand
Homewbrew installation, common commands and installation path
各大框架都在使用的Unsafe类,到底有多神奇?
Jour (9) de ramassage de MATLAB
捡起MATLAB的第(5)天
G008-HWY-CC-ESTOR-04 华为 Dorado V6 存储仿真器配置
TIA botu - basic operation
Using JSON server to create server requests locally
随机推荐
捡起MATLAB的第(9)天
深度学习100例 | 第41天-卷积神经网络(CNN):UrbanSound8K音频分类(语音识别)
About JMeter startup flash back
GRBL学习(二)
Qipengyuan horizon credible meta universe social system meets diversified consumption and social needs
欣旺达:HEV和BEV超快充拳头产品大规模出货
捡起MATLAB的第(5)天
Differences between MySQL BTREE index and hash index
Matplotlib tutorial 05 --- operating images
Solution of garbled code on idea console
文件系统读写性能测试实战
Detailed explanation of gzip and gunzip decompression parameters
04 Lua operator
Solution to the fourth "intelligence Cup" National College Students' IT skills competition (group B of the final)
第九天 static 抽象类 接口
What does cloud disaster tolerance mean? What is the difference between cloud disaster tolerance and traditional disaster tolerance?
Summary according to classification in sail software
JSP learning 3
The first line and the last two lines are frozen when paging
MySQL的btree索引和hash索引区别