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

相关内容

热门资讯

今日重大通报!hhpoker辅... 今日重大通报!hhpoker辅助挂下载,wepoker怎么看牌型,线上教程(有挂方法);支持多人共享...
透视了解!有没有人wepoke... 透视了解!有没有人wepoker(透视)确实是有挂(详细辅助细节揭秘)1、ai机器人多个强度级别选择...
专业讨论!佛手大菠萝有挂吗,w... 专业讨论!佛手大菠萝有挂吗,wepoker手机版辅助,必胜教程(有挂攻略);佛手大菠萝有挂吗软件透明...
透视安卓版!哈糖大菠萝有没有挂... 透视安卓版!哈糖大菠萝有没有挂(透视)原来是有挂(详细辅助透明挂教程)辅助器中分为三种模型:软件透明...
专业讨论!德州真人透视脚本,红... 专业讨论!德州真人透视脚本,红龙poker有辅助吗,新2025教程(有挂教程)1、许多玩家不知道德州...
透视神器!德普之星有辅助软件吗... 透视神器!德普之星有辅助软件吗(透视)其实有挂(详细辅助必备教程);一、软件透明挂的定义与意义1、系...
总算了解!wepoker透视器... 自定义wepoker透视器免费系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助...
透视苹果版!hhpoker辅助... 透视苹果版!hhpoker辅助挂(透视)总是存在有挂(详细辅助靠谱教程)辅助器中分为三种模型:软件透...
今日焦点!wepoker免费脚... 今日焦点!wepoker免费脚本弱密码,wepokerplus外挂,曝光教程(有挂技巧)1、很好的工...
透视规律!wpk作弊(透视)原... 透视规律!wpk作弊(透视)原来真的有挂(详细辅助科技教程)1、点击下载安装,插件透视分类一目了然!...