0

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);
}
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
ziha
  • 1
  • 1
  • 2
  • 1
    Do you want one signature per page or one global signature with visualization on each page? – mkl Mar 28 '13 at 07:35
  • one global signature with visualization on each page.. – ziha Mar 28 '13 at 07:46
  • Cf. My answer, that is not possible with iText as is (it is possible to extend iText appropriately, though). Furthermore, the legal value of such signatures with multiple visualizations is questionable and PDF viewers may, therefore, begin rejecting such signatures in future. – mkl Mar 28 '13 at 08:17

2 Answers2

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.

mkl
  • 90,588
  • 15
  • 125
  • 265
  • do you mean it is impossible to use same signature in multiple page? – ziha Mar 28 '13 at 08:32
  • 1
    No, i said something different: iText as is does not support the *easy* creation of such signatures out of the box, and PDF viewers might *in future* decide to reject such signatures due to legal concerns. It's *not* impossible. – mkl Mar 28 '13 at 09:47
1

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.

Jeevanantham
  • 984
  • 3
  • 19
  • 48