使用iText在每个页面中进行数字签名 [英] Digital signature in each page using iText
问题描述
我使用iReport生成了一个包含3页的报告.现在,签名仅出现在一页中.但是我需要使用iText对每个页面进行签名.
I have generated a report which have 3 pages using iReport. Now the signature only appears in one page. But I need to sign each page using iText.
PdfReader reader = new PdfReader(fullFilePath);
String outputPath = reportPath + randomUUID + fileExtension;
FileOutputStream fout = new FileOutputStream(outputPath);
stp = PdfStamper.createSignature(reader, fout, '\0', null, true);
PdfSignatureAppearance sap = stp.getSignatureAppearance();
sap.setCrypto(key, chain, null,PdfSignatureAppearance.SELF_SIGNED);
sap.setReason("test");
sap.setLocation("test");
String imagePath = servletContextPath + "/resources/img/signature.png";
File file = new File(imagePath);
byte[] imageByte = new byte[(int) file.length()];
try (FileInputStream fileInputStream = new FileInputStream(file)) {
fileInputStream.read(imageByte);
Image image = Image.getInstance(imageByte);
sap.setImage(image);
}
// comment next line to have an invisible signature
for (int page = 1; page <= reader.getNumberOfPages(); page++){
sap.setVisibleSignature(signatureRectangle, page, null);
}
推荐答案
除法律问题外,如果您仍然希望使用Itext api(版本5.5.*)对所有页面进行签名,则应在<PdfSignatureAppearance
类的c0>方法,该方法在页面中包含签名外观.
Apart from legal issues, if you still want to sign all the pages with Itext api (version 5.5.*) , they you should do a little hack in preClose(HashMap<PdfName, Integer> exclusionSizes)
method of PdfSignatureAppearance
class which where the signature appearance is included in the pages.
在PdfSignatureAppearance
类内搜索writer.addAnnotation(sigField, pagen);
行并替换为
for (int p = 1; p <= writer.reader.getNumberOfPages(); p++) {
writer.addAnnotation(sigField, p);
}
它将签名的引用添加到所有页面.
It add the reference of the signature to all the pages.
这篇关于使用iText在每个页面中进行数字签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!