AndroidiTextPdfSignatureField如何实现数字签名?
创始人
2024-10-08 09:01:45
0次
  1. 在项目中添加iTextPDF库的依赖。在这里我使用了iText7库。
implementation 'com.itextpdf:itext7-core:7.1.15'
implementation 'com.itextpdf:itext7-sign:7.1.15'
  1. 在要添加签名的PDF文档中添加FormField。FormField可以用来添加签名数字,并且iText7库也提供了几种类型的签名域来覆盖使用场景。
PdfAcroForm acroForm = PdfAcroForm.getAcroForm(pdfDocument, true);
PdfSignatureFormField field = PdfFormField.createSignature(pdfDocument, rect);
field.setFieldName("signature");
field.getWidgets().get(0).setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT);
acroForm.addField(field);
  1. 为签名设置属性和位置。
field.setValue("digitally signed by Jerome");
field.setFieldName("Signature");
field.setName("Jerome");
field.setLocation("Ghent");
field.setReason("Testing iText signing");
field.setContactInfo("test@example.com");
PdfDictionary dict = new PdfDictionary();
dict.put(PdfName.Filter, PdfName.Adobe_PPKLite);
dict.put(PdfName.SubFilter, PdfName.Adbe_pkcs7_detached);
field.setCryptoDictionary(dict);
  1. 创建签名的证书和密钥对,然后使用私钥对PDF进行签名。
String keystorePath = "/path/to/keystore.jks";
String password = "your_password";
String alias = "your_alias";
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(new FileInputStream(keystorePath), password.toCharArray());

PrivateKey pk = (PrivateKey) ks.getKey(alias, password.toCharArray());
Certificate[] chain = ks.getCertificateChain(alias);

PdfSigner signer = new PdfSigner(new PdfReader("input.pdf"), os, new StampingProperties());
PdfSignatureAppearance appearance = signer.getSignatureAppearance();
appearance.setReason("Test");
appearance.setLocation("Ghent");
appearance.setPageNumber(1);
appearance.setCertificate(chain[0]);

SignUtils signUtils = new SignUtils(pk, chain, "SHA-256

相关内容

热门资讯

必备教程!微乐科技软件下载(辅... 必备教程!微乐科技软件下载(辅助器),微信微乐卡五星本来有透视挂插件1、下载好微乐科技软件下载透视辅...
每日必看教程!微乐自建房脚本最... 每日必看教程!微乐自建房脚本最新版本更新内容(外挂),广西微乐麻将一直存在有透视挂脚本1、下载好微乐...
玩家攻略推荐!微信小程序微乐跑... 玩家攻略推荐!微信小程序微乐跑得快技巧(辅助),微乐新余50k有一直存在有透视挂工具小薇(辅助器软件...
教程攻略!微信小程序微乐辅助器... 教程攻略!微信小程序微乐辅助器真吗(开挂),微乐斗地主填大坑一直有透视挂工具1、微信小程序微乐辅助器...
揭秘真相!微乐小程序辅助器脚本... 揭秘真相!微乐小程序辅助器脚本(开挂),陕西微乐麻将一直是有透视挂app1、上手简单,内置详细流程视...
推荐几款新版!微信小程序微乐安... 推荐几款新版!微信小程序微乐安徽辅助苹果(作弊器),微乐山西麻将果然是有透视挂软件1、玩家可以在微信...
推荐攻略!微乐老友广东辅助器(... 推荐攻略!微乐老友广东辅助器(透视器),微乐明皇暗保一直存在有透视挂技巧1)微乐老友广东辅助器免费钻...
科技分享!微乐陕西小程序破解器... 科技分享!微乐陕西小程序破解器下载(作弊器),微乐湖北麻将一直有透视挂教程1、进入游戏-大厅左侧-新...
大神推荐!微乐小程序黑科技下载... 大神推荐!微乐小程序黑科技下载(透视挂),微乐海南麻将其实真的是有透视挂脚本1、操作简单,无需微乐小...
今日重大通报!微乐自建房透视(... 今日重大通报!微乐自建房透视(透视器),微乐宁德麻将本来是真的有透视挂插件微乐自建房透视辅助器是一种...