当前位置: 首页 > news >正文

pdf使用pdfbox切割pdf文件MultipartFile

引入依赖:

<dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.25</version></dependency>

测试代码:


import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.swagger.annotation.Permission;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;/*** <p>* description xxxx* </p>** @author liqin 2024/4/15 14:53*/
@RestController("TestOcrController.v1")
@RequestMapping("/v1/{organizationId}/ocr-test")
public class TestOcrController {@Permission(level = ResourceLevel.ORGANIZATION)@PostMappingpublic void ocrMain(@RequestParam("file") MultipartFile file) {String fileName = file.getOriginalFilename();System.out.println("文件类型:{}"+ fileName);String contentType = file.getContentType();System.out.println("contentType:{}"+ contentType);int dotIndex = fileName.lastIndexOf('.');String filePrefixName = "";String fileType = "";if (dotIndex != -1) {fileType = fileName.substring(dotIndex + 1).toLowerCase();filePrefixName = fileName.substring(0, fileName.lastIndexOf("."));}System.out.println("文件类型:{}"+ fileType);PDDocument doc = null;try (InputStream is = file.getInputStream()) {doc = PDDocument.load(is);int pageCount = doc.getNumberOfPages();System.out.println("pageCount:"+ pageCount);for (int i = 0; i < pageCount; i++) {// PDF 按页切割 还是生成pdf  ======================================================PDDocument outputDocument = new PDDocument();PDPage page = doc.getPage(i);outputDocument.addPage(page);try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {outputDocument.save(outputStream);byte[] bytes = outputStream.toByteArray();// 使用MockMultipartFile将byte[]转换为MultipartFileString newOriginalFilename = filePrefixName+"-"+(i+1)+"."+fileType;MockMultipartFile multipartFile = new MockMultipartFile("file",newOriginalFilename,"application/pdf",bytes);// 处理multipartFile,例如保存到磁盘或上传String path = "C:\\Users\\Administrator\\Desktop\\fileTest"+ File.separator +newOriginalFilename;multipartFile.transferTo(new File(path));}finally {try {if(outputDocument != null) {// 这里需要关闭PDDocument,不然如果想要删除pdf文件时会提示文件正在使用,无法删除的情况outputDocument.close();}} catch (IOException e) {e.printStackTrace();}}// PDF 每一页转图片 ======================================================
//                ByteArrayOutputStream opStream = new ByteArrayOutputStream();
//                PDFRenderer renderer = new PDFRenderer(doc);
//                BufferedImage image = renderer.renderImageWithDPI(i, 200, ImageType.ARGB);
//                ImageIO.write(image, "JPG", opStream);
//                String newOriginalFilename = filePrefixName+"-"+(i+1)+".jpg";
//                MultipartFile multipartFile = new MockMultipartFile("file",
//                        newOriginalFilename,
//                        "image/jpg", opStream.toByteArray());
//                System.out.println("新文件大小:{}"+ multipartFile.getSize());
//
//                // 处理multipartFile,例如保存到磁盘或上传
//                // 目标文件路径
//                String path = "C:\\Users\\Administrator\\Desktop\\fileTest"+ File.separator +newOriginalFilename;
//                System.out.println("地址:{}"+ path);
//                Path filePath = Paths.get(path);
//                // 将MultipartFile内容写入到目标文件
//                Files.copy(multipartFile.getInputStream(), filePath);}}catch (Exception e){e.printStackTrace();}finally {try {if(doc != null) {// 这里需要关闭PDDocument,不然如果想要删除pdf文件时会提示文件正在使用,无法删除的情况doc.close();}} catch (IOException e) {e.printStackTrace();}}}}
http://www.lryc.cn/news/355206.html

相关文章:

  • 力扣HOT100 - 31. 下一个排列
  • 设计模式 20 中介者模式 Mediator Pattern
  • 在 C++ 中,p->name 和 p.name 的效果并不相同。它们用于不同的情况,取决于你是否通过指针访问结构体成员。
  • C++基础:多态
  • 移除元素(算法题)
  • 电商场景的视频动效
  • Windows操作系统基本知识整理
  • Vue 状态管理深入研究:Vuex 和 Pinia 的原理与实践对比
  • 【三数之和】python,排序+双指针
  • TCP通信实现(服务端与客户端)
  • 安装appium自动化测试环境,我自己的版本信息
  • 【讲解下Web前端三大主流的框架】
  • 视频监控平台AS-V1000产品介绍:账户或用户数据的导入和导出功能介绍
  • markdown画时序图的时候,如何自动显示每一条时序的序号
  • 朴素贝叶斯
  • 【软件设计师】——10.面向对象技术
  • 唐山无人机航拍,唐山无人机建模,唐山数据孪生
  • vue中分页查询的实现
  • 类 和 对象(二)
  • buu[HCTF 2018]WarmUp(代码审计)
  • 力扣爆刷第145天之图论五连刷(dfs和bfs)
  • Host头攻击-使用加密和身份验证机制
  • 衍生品赛道的 UniSwap:SynFutures 或将成为行业领军者
  • TypeScript中的`let`、`const`、`var`区别:变量声明的规范与实践
  • 【python】python商家会员数据分析可视化(源码+数据集+课程报告论文)
  • Python限制输入的数范围
  • postman都有哪些功能?
  • 华为ensp中USG6000V防火墙双机热备VRRP+HRP原理及配置
  • ROS for LabVIEW:实现LabVIEW与ROS的无缝集成
  • yolov8+ROS+ubuntu18.04——学习记录