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

网站增加栏目费用成都公司建站模板

网站增加栏目费用,成都公司建站模板,湛江自助建站模板,做个产品网站要多少钱一、前言 项目以前签字都是由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.hrbkazy.com/news/15205.html

相关文章:

  • 遵义网站搭建公司哪家好网上宣传广告怎么做
  • 做网站什么职业seo是搜索引擎营销吗
  • 南京模板建站哪家好关联词有哪些关系
  • 网站外链建设到底该怎么做温州seo博客
  • 现在什么语言做网站最好电商网站模板
  • 做做网站2023下载建网站教程
  • 天津网站建设维护深圳市社会组织总会
  • 丢盖网logo设计免费福州关键词排名优化
  • 哪里有做直销网站的成都百度提升优化
  • 网站降权该怎么做公司网站建设哪家公司好
  • 网站建设有名的公司推广怎么做才可以赚钱
  • 做网站和做平台的区别seo管理系统创作
  • 做网站 ecs vps谷歌广告推广
  • 做网站路径如何优化网站快速排名
  • 聊城做网站信息厦门seo百度快照优化
  • 衡阳做网站的南宁网站seo优化公司
  • 网站建设平台源码外包公司被辞退有补偿吗
  • 自己电脑做网站还用备案google推广seo
  • 文字图片制作网站百度销售系统
  • 做网站挂靠点击器原理
  • 服务器硬件影响网站速度长尾关键词
  • 太原病毒最新消息seo优化的常用手法
  • 扬州建设工程信息网站怎么推广
  • 网站设计说明书怎么写深圳推广系统
  • 5人免费crm咸宁网站seo
  • 可以上传资源的网站开发费用100个免费推广b站
  • 做网站 内网怎么映射企业网站推广模式
  • 网络设计工程师是做什么的上海高端seo公司
  • 莱阳做网站网络推广公司可不可靠
  • 全是广告的网站网络营销成功案例有哪些2022