当前位置:网站首页>itextpdf实现多PDF文件合并为一个PDF文档
itextpdf实现多PDF文件合并为一个PDF文档
2022-07-25 17:42:00 【一恍过去】
1、引入POM
<dependencies>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.3</version>
</dependency>
</dependencies>
2、编写工具类
public class PdfUtils {
/** * 创建pdf文档 * * @param response * @param fileName * @return */
public static Document createDocument(HttpServletResponse response, String fileName) {
try {
response.reset();
response.setHeader("Content-Type", "application/pdf-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
} catch (Exception e) {
e.printStackTrace();
}
// 设置大小
return new Document(PageSize.A4, 50, 50, 80, 50);
}
/** * 合并pdf * * @param response * @param document * @param byteList */
public static void mergePdf(HttpServletResponse response, Document document, List<byte[]> byteList) {
try {
OutputStream os = response.getOutputStream();
// 以任意一个页面创建pdf模板
document = new Document(new PdfReader(byteList.get(0)).getPageSize(1));
PdfCopy copy = new PdfCopy(document, os);
// 打开文档
document.open();
// 遍历pdf文件
for (byte[] bytes : byteList) {
// 读取pdf
PdfReader reader = new PdfReader(bytes);
// 获取页数
int numberOfPages = reader.getNumberOfPages();
// 从第一页开始
for (int i = 1; i <= numberOfPages; i++) {
// 新建文档页
document.newPage();
// 复制操作
PdfImportedPage page = copy.getImportedPage(reader, i);
copy.addPage(page);
}
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("PDF合并失败!");
} finally {
if (document != null) {
document.close();
}
}
}
}
3、代码实现
支持上传MultipartFile文件或者通过本地文件的Path的进行合并PDF
@RestController
@RequestMapping("/test")
public class PdfController {
/** * 上传文件进行合并 * * @param files * @param response */
@PostMapping("pdfStream")
public void pdf(@RequestParam("files") MultipartFile[] files, HttpServletResponse response) {
try {
// 判断文件是否合规、以及转化为输入流
List<byte[]> byteList = new ArrayList<>();
for (MultipartFile file : files) {
// 限制格式只能为pdf
if (file == null) {
throw new RuntimeException("文件不能为空!");
}
String originalFilename = file.getOriginalFilename();
String substring = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
if (!substring.equals("pdf")) {
throw new RuntimeException("只能上传pdf格式文件!");
}
// 获取输入流
// 获取输入流
InputStream inputStream = file.getInputStream();
byte[] bytes = inputStreamToByte(inputStream);
byteList.add(bytes);
}
// 1.设置输出流名称
String fileName = "合并文件.pdf";
Document document = PdfUtils.createDocument(response, fileName);
// 2.开启合并
PdfUtils.mergePdf(response, document, byteList);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
/** * 本地文件转换合并 * * @param response */
@GetMapping("pdfPath")
public void pdfPath(HttpServletResponse response) {
try {
// 加载本地文件
List<String> paths = new ArrayList<>();
paths.add("C:\\Users\\LiGezZ\\Desktop\\1.pdf");
paths.add("C:\\Users\\LiGezZ\\Desktop\\2.pdf");
// 判断文件是否合规、以及转化为输入流
List<byte[]> byteList = new ArrayList<>();
for (String path : paths) {
File file = new File(path);
// 限制格式只能为pdf
if (!file.exists()) {
throw new RuntimeException("文件不存在!");
}
if (file.length() <= 0) {
throw new RuntimeException("文件不能为空!");
}
String name = file.getName();
String substring = name.substring(name.lastIndexOf(".") + 1);
if (!substring.equals("pdf")) {
throw new RuntimeException("只能上传pdf格式文件!");
}
// 获取输入流
FileInputStream inputStream = new FileInputStream(file);
byte[] bytes = inputStreamToByte(inputStream);
byteList.add(bytes);
}
// 1.设置输出流名称
String fileName = "合并文件.pdf";
Document document = PdfUtils.createDocument(response, fileName);
// 2.开启合并
PdfUtils.mergePdf(response, document, byteList);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
private byte[] inputStreamToByte(InputStream inputStream) {
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
os.write(bytes, 0, len);
}
os.flush();
return os.toByteArray();
} catch (Exception e) {
throw new RuntimeException("InputStream转换失败!");
}
}
}
边栏推荐
- OSPF综合实验
- Interface automation test postman+newman+jenkins
- 对灰度图像的三维函数显示
- "Digital security" alert NFT's seven Scams
- Hcip first day experiment
- WPF 实现用户头像选择器
- [cadence Allegro PCB design] error: possible pin type conflict gnd/vcc power connected to output
- 【VSCODE】支持argparser/接受命令行参数
- 栈的顺序存储结构,链式存储结构及实现
- Headless mode of new selenium4.3 in egde browser
猜你喜欢
随机推荐
Enumeration classes and magic values
[solution] the Microsoft edge browser has the problem of "unable to access this page"
New and malloc
[Hardware Engineer] about signal level driving capability
OSPF---开放式最短优先路径协议
Summary of knowledge points for final review of server-side architecture design
霸榜COCO!DINO: 让目标检测拥抱Transformer
食品安全 | 八问八答带你重新认识小龙虾!这样吃才对!
Redis源码与设计剖析 -- 16.AOF持久化机制
网上开期货账户安全吗?手续费怎么申请才低?
Go language context control function execution timeout return
mongodb 集群及分片
Go channel simple notes
计算日期或日期格式化
Trooper
ROS learning notes (IV) ROS cannot solve rosdep init or update
8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇
Pymongo saves data in dataframe format (insert_one, insert_many, multi-threaded saving)
[Hardware Engineer] can't select components?
交叉验证(cv)学习笔记









