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

记录使用iText7查找PDF内容关键字坐标,加盖电子签名、印章

一、前言

项目以前签字都是由C端那边进行合成操作,最近项目要求把那块功能,由后端进行实现,其中包含坐标、关键字、任意位置进行签字操作,坐标是最容易实现的,曾经也写过类似的功能在(添加图片印章到PDF)直接复用就可以了
为了实现关键字位置签字,在网上查找了挺多资料的,感觉能满足功能的代码参考地址:Itext7获取关键字在文件中的坐标
在他这基础上我进行了优化

二、使用JAR包

<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13.1</version>
</dependency>
<dependency><groupId>com.itextpdf</groupId><artifactId>itext7-core</artifactId><version>7.2.0</version><type>pom</type>
</dependency>

三、实现代码

import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.canvas.parser.PdfCanvasProcessor;
import com.itextpdf.kernel.pdf.canvas.parser.listener.IPdfTextLocation;
import com.itextpdf.kernel.pdf.canvas.parser.listener.RegexBasedLocationExtractionStrategy;import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.*;public class PDFKeywordFinder {public static Map<String, Object> getKeywordLocation(ByteArrayInputStream bytes, String keyword) {Map<String, Object> result = new HashMap<>();//使用这种方式,无需再关闭了,处理完自动关闭try (PdfReader reader = new PdfReader(bytes); PdfDocument pdfDocument = new PdfDocument(reader)) {for (int i = 1; i <= pdfDocument.getNumberOfPages(); i++) {PdfPage page = pdfDocument.getPage(i);RegexBasedLocationExtractionStrategy strategy = new RegexBasedLocationExtractionStrategy(keyword);PdfCanvasProcessor canvasProcessor = new PdfCanvasProcessor(strategy);canvasProcessor.processPageContent(page);Collection<IPdfTextLocation> resultantLocations = strategy.getResultantLocations();// 自定义结果处理if (!resultantLocations.isEmpty()) {List<Map<String, Object>> locationList = new ArrayList<>();for (IPdfTextLocation item : resultantLocations) {Map<String, Object> map = new HashMap<>();Rectangle rectangle = item.getRectangle();map.put("page", item.getPageNumber());map.put("absoluteX", rectangle.getX());map.put("absoluteY", rectangle.getY());map.put("width", rectangle.getWidth());map.put("height", rectangle.getHeight());map.put("keyword", item.getText());map.put("top", rectangle.getTop());map.put("bottom", rectangle.getBottom());map.put("left", rectangle.getLeft());map.put("right", rectangle.getRight()); // 右坐标map.put("count", locationList.size() + 1); // 出现次数locationList.add(map);}result.put(String.valueOf(i), locationList);}}return result;} catch (IOException e) {throw new RuntimeException(e);}}
}

四、测试用例

本地测试用了文件转内存流方式,如果需要加盖电子印章,可以把签字图片替换成电子印章图片
我这没用到签名密钥,如果需要可以自行添加,或者找其它的处理方式

//引入包信息
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfStamper;import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;public static void main(String[] args) throws IOException, DocumentException {String filePath = "E:\\test.pdf";String keyword = "签收人"; // 要查找的关键字ByteArrayOutputStream memoryStream = readFileToMemoryStream(filePath);Map<String, Object> map = getKeywordLocation(new ByteArrayInputStream(memoryStream.toByteArray()), keyword);com.itextpdf.text.pdf.PdfReader pdfReader = new com.itextpdf.text.pdf.PdfReader(memoryStream.toByteArray());PdfStamper pdfStamper = new PdfStamper(pdfReader, Files.newOutputStream(Paths.get("E:\\文书.pdf")));com.itextpdf.text.Image image = Image.getInstance("E:\\sign.png");//设置签字图片宽高image.scaleAbsolute(100, 50);//我直接在第一页操作,也可以根据返回的数据进行操作JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(map.get("1")));JSONObject jsonObject = jsonArray.getJSONObject(0);//这个位置主要是插入签名图片位置坐标,也可以根据情况自行调整取值float absoluteX = Float.parseFloat(jsonObject.getString("absoluteX")) - Float.parseFloat(jsonObject.getString("width"));float absoluteY = Float.parseFloat(jsonObject.getString("absoluteY")) - (Float.parseFloat(jsonObject.getString("width")) + Float.parseFloat(jsonObject.getString("height")));image.setAbsolutePosition(absoluteX, absoluteY);PdfContentByte content = pdfStamper.getUnderContent(1);content.addImage(image);pdfStamper.close();
}private static ByteArrayOutputStream readFileToMemoryStream(String filePath) throws IOException {ByteArrayOutputStream memoryStream = new ByteArrayOutputStream();byte[] buffer = new byte[1024]; // 缓冲区大小try (FileInputStream fis = new FileInputStream(filePath)) {int bytesRead;while ((bytesRead = fis.read(buffer)) != -1) {memoryStream.write(buffer, 0, bytesRead);}}return memoryStream;
}

五、效果展示

在这里插入图片描述

http://www.lryc.cn/news/177757.html

相关文章:

  • Java8实战-总结37
  • 【超详细】前段开发之详细的Vue3入门教程,特别适合小白系统学习,入门到熟练使用Vue看这一篇就够了!
  • 【深度学习】ONNX模型多线程快速部署【基础】
  • Python 同、异步HTTP客户端封装:性能与简洁性的较量
  • 无代码赋能数字化,云表搭桥铺路链接“数据孤岛”
  • 无需公网IP,实现公网SSH远程登录MacOS【内网穿透】
  • 网络爬虫学习笔记 1 HTTP基本原理
  • 113. 路径总和ii
  • 百度APP iOS端包体积50M优化实践(六)无用方法清理
  • MySQL了解视图View (视图篇 一)
  • 使用applescript自动化trilium的数学公式环境
  • idea中maven项目打包成jar,报错没有主清单属性解决方法
  • Caddy Web服务器深度解析与对比:Caddy vs. Nginx vs. Apache
  • 基于PHP+MySQL的家教平台
  • 吉利微型纯电,5 万元的快乐
  • Gitee使用方法
  • 前端适配笔记本缩放125%,150%导致页面错乱问题
  • 多线程的学习中篇下
  • 贪心算法-拼接字符串使得字典顺序最小问题
  • Linux--互斥锁
  • [2023.09.21]:源码已上传,供大家了解Rust Yew的前后端开发
  • 时序分解 | Matlab实现CEEMD互补集合经验模态分解时间序列信号分解
  • linux缓存-利用缓存提高性能的编程技巧
  • Socks5代理、IP代理与其在爬虫开发中的应用
  • 【C++】C++继承——切片、隐藏、默认成员函数、菱形
  • WebGL笔记:WebGL中绘制圆点,设定透明度,渲染动画
  • 华为云云耀云服务器L实例评测 | 实例使用教学之简单使用:通过命令行管理华为云云耀云服务器
  • 微信小程序 课程签到系统
  • 如何用Postman做接口自动化测试
  • 支付宝电脑网站支付,异步通知