当前位置:网站首页>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
边栏推荐
- The solution of not displaying a whole line when the total value needs to be set to 0 in sail software
- 撿起MATLAB的第(9)天
- Sail soft implements a radio button, which can uniformly set the selection status of other radio buttons
- ESP32_Arduino
- 捡起MATLAB的第(9)天
- Win11 / 10 home edition disables the edge's private browsing function
- 安装及管理程序
- Gartner predicts that the scale of cloud migration will increase significantly; What are the advantages of cloud migration?
- Day 10 abnormal mechanism
- JSP learning 1
猜你喜欢

阿里研发三面,面试官一套组合拳让我当场懵逼

OAK-D树莓派点云项目【附详细代码】

ESP32编译环境的搭建

Nanny Anaconda installation tutorial

Set cell filling and ranking method according to the size of the value in the soft report

捡起MATLAB的第(4)天

Disk management and file system

Sail soft calls the method of dynamic parameter transfer and sets parameters in the title

How magical is the unsafe class used by all major frameworks?

Force buckle - 198 raid homes and plunder houses
随机推荐
Es common query, sorting and aggregation statements
Gartner predicts that the scale of cloud migration will increase significantly; What are the advantages of cloud migration?
Hypermotion cloud migration completes Alibaba cloud proprietary cloud product ecological integration certification
安装及管理程序
Sail soft implements a radio button, which can uniformly set the selection status of other radio buttons
MySQL的btree索引和hash索引区别
How to upgrade openstack across versions
Grbl learning (II)
撿起MATLAB的第(9)天
Take according to the actual situation, classify and summarize once every three levels, and see the figure to know the demand
Oracle data pump usage
Cloud migration practice in the financial industry Ping An financial cloud integrates hypermotion cloud migration solution to provide migration services for customers in the financial industry
欣旺达:HEV和BEV超快充拳头产品大规模出货
第九天 static 抽象类 接口
[key points of final review of modern electronic assembly]
Day (8) of picking up matlab
阿里研发三面,面试官一套组合拳让我当场懵逼
VIM uses vundle to install the code completion plug-in (youcompleteme)
JSP learning 2
面试题 17.10. 主要元素