使用的pom

<!-- pdf处理 start-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/kernel -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>7.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/layout -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>7.1.2</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.49</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/forms -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>forms</artifactId>
<version>7.1.4</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.49</version>
</dependency>
<!-- pdf处理 end-->

效果图

原理

  1. 通过itext中List添加固定文本
  2. 添加指定标记比如★☆用以之后替换成其他文本或图片
  3. 可以不用标记,反正就是算好位置
  4. 至于签章这块位置的选定,根据文档最后一行位置判定,我的判定方法就是文档最后一页最后一行离尾部距离小于一定值,签章的整块内容移到新的一页

过程

  1. 核心利用了com.itextpdf.text.pdf.parser.RenderListener这个类,它会遍历这个文档的内容
  2. 写个继承这个类的方法,实现方法如下
@Override
public void renderText(TextRenderInfo textInfo) {
Float bound = textInfo.getBaseline().getBoundingRectange();
String text = textInfo.getText();
float y = bound.y - this.fixHeight;
for (String keyWord : findText) {
if (null != text && text.contains(keyWord)) {
ReplaceRegion region = new ReplaceRegion(keyWord);
region.setH(bound.height == 0 ? defaultH : bound.height);
if ((text.contains("☆") && keyWord.equals("☆"))) {
region.setW(20f);
int i = text.indexOf("☆");
region.setX(bound.x + 13 * i);
} else if (text.contains("★") && keyWord.equals("★")) {
region.setW(15f);
region.setX(bound.x);
} else if ((text.contains("△") && keyWord.equals("△"))) {
region.setW(15f);
int i = text.indexOf("△");
region.setX(bound.x + 11 * i);
} else if ((text.contains("▲") && keyWord.equals("▲"))) {
region.setW(15f);
region.setX(bound.width + 15 * 4.5f);
} else {
region.setW(bound.width);
region.setX(bound.x);
}
region.setY(y);
result.put(keyWord, region);
}
}
//判断最后一行是否小于某个值
if (y < heightSign) {
signY.put("endY", 0f);
} else {
signY.put("endY", y);
}
}
  1. 这里我进行了很多微调,此方法肯定存在很多改进的地方,由于时间紧急,我对itext的研究也不深,勉强实现需求
...
PdfReader reader = new PdfReader(pdfBytes);
//内容解析器
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
for (int i = 1; i <= numberOfPages; i++) {
parser.processContent(i, listener);
Map<String, ReplaceRegion> res = listener.getResult(i);
if (res.size() > 0) {
pdfReplacer.setPageNum(i);
result = res;
}
}
...
  1. 通过上面的步骤找到最后一行位置,找到指定特殊字符的位置
  2. 添加尾部签章部分通过
PdfReader reader = new PdfReader(basePath + "_temp2.pdf");
PdfWriter writer = new PdfWriter(basePath + "_temp3.pdf");
PdfDocument pdf = new PdfDocument(reader, writer);
int numberOfPages = pdf.getNumberOfPages();
float height = pdf.getDefaultPageSize().getHeight();
if (endY == 0f) {
numberOfPages++;
endY = height - 140f;
pdf.addNewPage();
}else{
endY = endY - 60f;
} Document doc = new Document(pdf);
PdfFont font = PdfFontFactory.createFont("C:/Windows/Fonts/simsun.ttc,1", PdfEncodings.IDENTITY_H,true);
com.itextpdf.layout.element.List list = new com.itextpdf.layout.element.List().setPageNumber(numberOfPages).setFixedPosition(80,endY,400f).setListSymbol("").setSymbolIndent(22f).setFont(font).setFontSize(14);
list.add(new ListItem("甲方法定代表人:☆ 乙方法定代表人: △"))
.add(new ListItem("联系电话: 联系电话:"))
.add(new ListItem("身份证号码: 身份证号码:"))
.add(new ListItem("★ ▲"));
doc.add(list); pdf.close();
  1. 整个过程会出现很多中间临时文件,所以说还是可以有很多改进的地方。

  2. 替换方法,用来替换日期,和覆盖特殊符号

textReplacer = new PdfReplacer(basePath + "_temp3.pdf");
textReplacer.replaceText(replaceStr, destStr.toString());
replaceRegion = textReplacer.toPdf(basePath + "_temp4.pdf");
PdfReplacer textReplacer2 = new PdfReplacer(basePath + "_temp4.pdf");
String dateRecord = sysconfig.getProperties().getProperty(dateSign);
String text = DateUtil.format2str("yyyy 年 MM 月 dd 日");
textReplacer2.setFont(14);
String dateFontPath = sysconfig.getProperties().getProperty("date_font_path");
if (dateFontPath.lastIndexOf("ttf") > 0) {
textReplacer2.setFont(new com.itextpdf.text.Font(BaseFont.createFont(dateFontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED)));
} else {
dateFontPath = dateFontPath + ",1";
textReplacer2.setFont(new com.itextpdf.text.Font(BaseFont.createFont(dateFontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED)));
}
log.info("字体路径{}", dateFontPath);
textReplacer2.replaceText(dateRecord, text);
  1. 签章方法
public static byte[] sign(String password, InputStream inputStream, String signPdfSrc, String signImage,
float x, float y,int page) {
File signPdfSrcFile = new File(signPdfSrc);
PdfReader reader = null;
ByteArrayOutputStream signPDFData = null;
PdfStamper stp = null;
try {
BouncyCastleProvider provider = new BouncyCastleProvider();
Security.addProvider(provider);
KeyStore ks = KeyStore.getInstance("PKCS12", new BouncyCastleProvider());
// 私钥密码 为Pkcs生成证书是的私钥密码 123456
ks.load(inputStream, password.toCharArray());
String alias = (String) ks.aliases().nextElement();
PrivateKey key = (PrivateKey) ks.getKey(alias, password.toCharArray());
Certificate[] chain = ks.getCertificateChain(alias);
reader = new PdfReader(signPdfSrc);
signPDFData = new ByteArrayOutputStream();
// 临时pdf文件
File temp = new File(signPdfSrcFile.getParent(), System.currentTimeMillis() + ".pdf");
stp = PdfStamper.createSignature(reader, signPDFData, '\0', temp, true);
stp.setFullCompression();
PdfSignatureAppearance sap = stp.getSignatureAppearance();
sap.setReason("数字签名,不可改变");
// 使用png格式透明图片
Image image = Image.getInstance(signImage);
sap.setImageScale(0);
sap.setSignatureGraphic(image);
sap.setRenderingMode(RenderingMode.GRAPHIC);
int size = 120;
// 是对应x轴和y轴坐标
float lly = y - 50;
sap.setVisibleSignature(new Rectangle(x, lly, x + size, lly+size), page,
UUID.randomUUID().toString().replaceAll("-", ""));
stp.getWriter().setCompressionLevel(5);
ExternalDigest digest = new BouncyCastleDigest();
ExternalSignature signature = new PrivateKeySignature(key, DigestAlgorithms.SHA512, provider.getName());
MakeSignature.signDetached(sap, digest, signature, chain, null, null, null, 0, CryptoStandard.CADES);
stp.close();
reader.close();
return signPDFData.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally { if (signPDFData != null) {
try {
signPDFData.close();
} catch (IOException e) {
}
} if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
}
}
}
return null;
}

itext实现合同尾部签章部分自动添加,定位签名的更多相关文章

  1. 前端自动化工具gulp自动添加版本号

    之前,我介绍了学习安装并配置前端自动化工具Gulp,觉得gulp确实比grunt的配置简单很多,于是我决定再深入学习一下gulp,就去网上查了资料,发现gulp还可以自动添加版本号,这个功能就为我平时 ...

  2. Gulp自动添加版本号(转载)

    本文转载自: gulp自动添加版本号

  3. VS 自动添加注释

    现在大多数公司都规定程序员在程序文件的头部加上版权信息,这样每个人写的文件都可以区分开来,如果某个文件出现问题就可以快速的找到文件的创建人,用最短的时间来解决问题,常常是以下格式: //======= ...

  4. Zabbix网络自动发现规则和自动添加hosts及link模板

    Version: zabbix 3.0 一.配置网络发现规则 Device uniqueness criteria:选择主机名作为唯一标识(Configuation Hosts中显示的NAME) 二. ...

  5. 如何给wordpress外部链接自动添加nofollow

    wordpress多作者博客可以丰富网站的内容,但同时也会产生一些无关的链接,例如有些投机的人会考虑在文章中随意添加外部链接,如果你不想给这些外部链接传递权重,你需要给这些外部链接加上 rel=&qu ...

  6. EditText中输入手机号码时,自动添加空格

    输入手机号码时,自动添加空格,更容易辨别 public class PhoneWatcher implements TextWatcher { private EditText _text; publ ...

  7. Excel VBA自动添加证书

    ---恢复内容开始--- 在说这个话题之前,我先解释一下为什么要加数字证书签名,它有什么作用,后面再解释如何添加.首先解释下证书添加的位置,如下图所示: 1.单击左上角的Office 按钮,选择右下角 ...

  8. 为js和css文件自动添加版本号

    web应用必然要面对缓存问题,无论前台后台都会涉足缓存.特别是对于前端而言,缓存利用的是否得当直接关系到应用的性能. 通常情况下,我们会倾向于使用缓存,因为缓存一方面可以减少网络开销,一方面可以减轻服 ...

  9. 向linux内核版本号添加字符/为何有时会自动添加“+”号

    转载:http://blog.csdn.net/adaptiver/article/details/7225980 1.   引子 编译2.6.35.7 kernel版本的时候发现,“2.6.35.7 ...

随机推荐

  1. 《Spring_Four》第三次作业——基于Jsoup的大学生考试信息展示系统的原型设计与开发

    <Spring_Four团队>第三次团队项目——基于Jsoup的大学生考试信息展示系统的原型设计与开发 一.实验目的与要求 (1)掌握软件原型开发技术: (2)学习使用软件原型开发工具:本 ...

  2. DOM 节点node

    DOM可以将任何HTML或XML文档描绘成一个有多层节点构成的结构,即在HTML中所有内容都是节点.文档节点是每个文档的根节点,文档节点有一个子节点,称为文档元素.每个文档只能有一个文档元素.在HTM ...

  3. $nextTick 的作用

    文档:深入响应式原理 Vue 实现响应式并不是数据发生变化之后 DOM 立即变化,而是按一定的策略进行 DOM 的更新. $nextTick 是在下次 DOM 更新循环结束之后执行延迟回调,在修改数据 ...

  4. 第一章 C++语言入门

            标准数据类型         C++语言提供了丰富的数据类型,如整数类型.实数类型(浮点数).字符类型等.每种数据类型均有均值范围,Dev-C++(4.9.9.2)是Windows平台 ...

  5. Android 滑块验证

    先上图看看实现效果 1.在 app 的 build.gradle 添加依赖 implementation 'com.luozm.captcha:captcha:1.1.2' 2.将 Captcha 添 ...

  6. 天天向上的力量 III

    描述 一年365天,以第1天的能力值为基数,记为1.0. 当好好学习时,能力值相比前一天提高N‰:当没有学习时,能力值相比前一天下降N‰. 每天努力或放任,一年下来的能力值相差多少呢?其中,N的取值范 ...

  7. Centos7编译安装lnmp(nginx1.10 php7.0.2)

    我使用的是阿里云的服务器 Centos7 64位的版本 1. 连接服务器 这个是Xshell5的版本 安装好之后我们开始连接服务器 2. 安装nginx 首先安装nginx的依赖 yum instal ...

  8. 【PHP面试题】通俗易懂的两个面试必问的排序算法讲解:冒泡排序和快速排序

    又到了金三银四找工作的时间,相信很多开发者都在找工作或者准备着找工作了.一般应对面试,我们无可厚非的去刷下面试题.对于PHPer来说,除了要熟悉自己所做的项目,还有懂的基本的算法.下面来分享下PHP面 ...

  9. linearlayout 中ImageView 居中等问题

    linearlayout  下的子控件使用android:layout_gravity=”center”  控件居左,没有达到居中的效果, 父窗体只能指定一种控件摆放方向 横向还是竖向 下面我弄了三个 ...

  10. 关于requests库中文编码问题

    转自:代码分析Python requests库中文编码问题 Python reqeusts在作为代理爬虫节点抓取不同字符集网站时遇到的一些问题总结. 简单说就是中文乱码的问题.   如果单纯的抓取微博 ...