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

惠州有家最好网站建设海外免费网站推广

惠州有家最好网站建设,海外免费网站推广,wordpress 头像本地化,房子已交房 建设局网站查不到一、前言 项目以前签字都是由C端那边进行合成操作,最近项目要求把那块功能,由后端进行实现,其中包含坐标、关键字、任意位置进行签字操作,坐标是最容易实现的,曾经也写过类似的功能在(添加图片印章到PDF&a…

一、前言

项目以前签字都是由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.mmbaike.com/news/43835.html

相关文章:

  • 合肥建设网站制作哪个好网络推广策划案
  • wordpress高级版湖南企业竞价优化服务
  • 综合门户网站有哪些来宾网站seo
  • 网站页脚优化怎么做农产品网络营销推广方案
  • 美国免费网站空间微信视频号可以推广吗
  • 微商做色情网站seo是什么意思
  • 广州力yang网站建设爱站网挖掘关键词
  • 举报网站建设情况汇报百度客服在哪里找
  • 编程网站免费中文版seo蜘蛛屯
  • 网站程序开发制作十大品牌如何推广自己的网站
  • 网上注册公司app安卓系统优化app
  • 网站首次备案网站建站公司
  • yellow在线观看高清完整版网站排名优化教程
  • 阳江房产网最新楼盘江楼盘青岛seo推广公司
  • 工作组赴河南协助关键词优化方法
  • 域名购买成功后网站怎么建设1元购买域名
  • 现在还做响应式网站吗seo关键词外包公司
  • 网站被网站建设的人控制了中国最新军事新闻
  • 网站建设原型图如何制作一个自己的网站
  • 企业标准化建设优化关键词排名软件
  • 行业门户型网站制作平台广告推广
  • 住房与城乡建设部网站注册中心百度一下网页版搜索引擎
  • 中职专业设计网站网络推广服务
  • 建设机械网站精英凡科网免费建站
  • 设计一套企业网站设计报价软文是什么意思通俗点
  • 手机网站无响应怎么建立自己的网站平台
  • 中文儿童网站模板网站建设及网络推广
  • 海外广告投放公司宁波seo公司推荐
  • ecs怎么添加wordpress太原百度搜索排名优化
  • 企业网站搜索推广全球中文网站排名