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

java通过pdf-box插件完成对pdf文件中图片/文字的替换

需要引入的Maven依赖:

  <!--    pdf替换图片    --><dependency><groupId>e-iceblue</groupId><artifactId>spire.pdf.free</artifactId><version>5.1.0</version></dependency>

java代码:

   public AjaxResult replacepdf(MultipartFile file)  {try {//初始化 PdfDocument 类的实例PdfDocument doc = new PdfDocument();PdfDocument doc1 = null;int fileType = 0;doc.loadFromStream(file.getInputStream());//加载 PDF 文档//region 单页模板替换if (doc.getPages().getCount() <= 1) {//获取所需的页面PdfPageBase page = doc.getPages().get(0);//region 获取原图大小  位置PdfImageInfo[] imagesInfo = page.getImagesInfo();//获取指定图片的坐标Rectangle2D bounds = imagesInfo[imagesInfo.length - 1].getBounds();double x = bounds.getX();double y = bounds.getY();double width = bounds.getWidth();double height = bounds.getHeight();//endregion//替换公章PdfImage image2 = PdfImage.fromFile(uploadPath + "//zryk//zrykseal//img//zrykseal.jpg");page.deleteImage(imagesInfo.length - 1);page.getCanvas().drawImage(image2, x + 40, y, 87, 87);PdfImage image = PdfImage.fromFile(uploadPath + "//zryk//zryklogo//img//zryk.jpg");page.replaceImage(0, image);}//endregion//region 文字替换for (Object pageFonts : doc.getPages()) {PdfPageBase pageF = (PdfPageBase) pageFonts;//region 需要替换的文字信息String oldPhone = "xxxx-25689581";String newPhone = "xxxx-4523 2358";String oldPhone1 = "25569861";String newPhone1 = "55586662";PdfTextFindCollection collection = pageF.findText(oldPhone, false);PdfTextFindCollection collection1 = pageF.findText(oldPhone1, false);//endregion// 指定替换文本String newText = newPhone;String newText1 = newPhone1;//region 查找并替换文本1for (Object findObj : collection.getFinds()) {PdfTextFind find = (PdfTextFind) findObj;// 获取文本在页面中的范围Rectangle2D.Float rec = (Rectangle2D.Float) find.getBounds();// 写入空白范围遮住原来的文本pageF.getCanvas().drawRectangle(PdfBrushes.getWhite(), rec);PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 10f);// 创建字体pageF.getCanvas().drawString(newText, font, PdfBrushes.getBlack(), rec.getX(), rec.getY());}//endregion//region 查找并替换文本2for (Object findObj : collection1.getFinds()) {PdfTextFind find = (PdfTextFind) findObj;// 获取文本在页面中的范围Rectangle2D.Float rec = (Rectangle2D.Float) find.getBounds();// 写入空白范围遮住原来的文本pageF.getCanvas().drawRectangle(PdfBrushes.getWhite(), rec);PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 10f);// 创建字体pageF.getCanvas().drawString(newText1, font, PdfBrushes.getBlack(), rec.getX(), rec.getY());}//endregion}//endregionreturn AjaxResult.success("上传成功");}catch(Exception e){return AjaxResult.error("上传失败,请联系管理员",JSON.toJSON(e.getStackTrace()));}}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 鸿蒙Navigation路由能力汇总
  • 自动化测试高级控件交互方法:TouchAction、触屏操作、点按,双击,滑动,手势解锁!
  • 编程的法则 迪米特法则(Law of Demeter)也称为“最少知识原则(Principle of Least Knowledge)包括如何实践
  • 微服务之间Feign调用
  • mac M1 创建Mysql8.0容器
  • C# Blazor Server与JS互操作无法获取大数据量返回值
  • 2024-07-13 Unity AI状态机2 —— 项目介绍
  • Dify中的高质量索引模式实现过程
  • 华为USG6000V防火墙NAT智能选举
  • Python和C++行人轨迹预推算和空间机器人多传感融合双图算法模型
  • 字节码编程之bytebuddy结合javaagent支持多种监控方式
  • 【Spring全家桶系列之核心篇 | Spring Cloud】 - 第七章 掌握Gateway核心技术,实现高效路由与转发
  • 灵雀云AML:赋能金融AI,构建数智时代核心竞争力
  • Android SurfaceView 组件介绍,挖洞原理详解
  • Apache httpd-vhosts.conf 配置详解(附Demo)
  • [微信小程序] 使用ES6特性Class后出现编译异常
  • 《Javascript数据结构和算法》笔记-「字典和散列表」
  • 「面试题」如何实现一个圣杯布局?
  • 【刷算法】求1+2+3+...+n
  • Babel配置的不完全指南
  • canvas实际项目操作,包含:线条,圆形,扇形,图片绘制,图片圆角遮罩,矩形,弧形文字...
  • export和import的用法总结
  • JavaScript工作原理(五):深入了解WebSockets,HTTP/2和SSE,以及如何选择
  • JavaScript函数式编程(一)
  • Java的Interrupt与线程中断
  • Lsb图片隐写
  • Making An Indicator With Pure CSS
  • MySQL数据库运维之数据恢复
  • Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel
  • 当SetTimeout遇到了字符串
  • 给第三方使用接口的 URL 签名实现
  • 关于for循环的简单归纳
  • 聊聊directory traversal attack
  • 聊聊spring cloud的LoadBalancerAutoConfiguration
  • 我看到的前端
  • 走向全栈之MongoDB的使用
  • ionic异常记录
  • k8s使用glusterfs实现动态持久化存储
  • Mac 上flink的安装与启动
  • UI设计初学者应该如何入门?
  • 国内开源镜像站点
  • ​力扣解法汇总1802. 有界数组中指定下标处的最大值
  • #{} 和 ${}区别
  • #LLM入门|Prompt#1.7_文本拓展_Expanding
  • #QT项目实战(天气预报)
  • #window11设置系统变量#
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • $var=htmlencode(“‘);alert(‘2“); 的个人理解
  • (Arcgis)Python编程批量将HDF5文件转换为TIFF格式并应用地理转换和投影信息
  • (c语言版)滑动窗口 给定一个字符串,只包含字母和数字,按要求找出字符串中的最长(连续)子串的长度
  • (ISPRS,2021)具有遥感知识图谱的鲁棒深度对齐网络用于零样本和广义零样本遥感图像场景分类
  • (Redis使用系列) SpringBoot 中对应2.0.x版本的Redis配置 一
  • (编译到47%失败)to be deleted
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (附源码)ssm考试题库管理系统 毕业设计 069043