问题
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);
}
回答1:
sap.setVisibleSignature(signatureRectangle, page, null);
sets the visual representation, it doesn't add another one. Therefore all your calls of this method in your loop but the last one are futile.
IText signature creation code as is can only create a single visual representation per signature, and while it is possible according to the specification to have e.g. multiple widgets visualizing the same signature, PDF viewers may reject it as the legal value of a signature with multiple visualizations is questionable.
EDIT:
Adobe, e.g., in their Digital Signature Appearances v9 white paper write:
The location of a signature within a document can have a bearing on its legal meaning. For this reason, signature fields never refer to more than one annotation. If more than one location is associated with a signature, the meaning may become ambiguous.
Thus, they may, in the future, stop accepting signatures with multiple annotations (i.e. visualisations) altogether.
回答2:
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.
search for writer.addAnnotation(sigField, pagen);
line inside PdfSignatureAppearance
class and replace with
for (int p = 1; p <= writer.reader.getNumberOfPages(); p++) {
writer.addAnnotation(sigField, p);
}
It add the reference of the signature to all the pages.
来源:https://stackoverflow.com/questions/15673274/digital-signature-in-each-page-using-itext