0

Everyone we required some help to resolve our problem. We are trying to sign all pages in a PDF using iText Java but unable to get proper result. Signing all pages with same signature is not happening and signing is happening last page of the PDF document.

Also we have tried with addAnnotation method like

stamper.addAnnotation(fieldSign, p)

Here i have added tried code snippet.

String sourcefile= "User_Application_withoutsign.pdf";
System.out.println("Path--->"+sourcefile);
destFile= "User_Application_signed.pdf";
reader =  new PdfReader(sourcefile);

Rectangle cropBox = reader.getCropBox(1);
Rectangle rectangle = null;
String user=null;
rectangle = new Rectangle(cropBox.getLeft(50),cropBox.getBottom(), cropBox.getLeft(500),cropBox.getBottom(100));
//rectangle.setWi
fout = new FileOutputStream(destFile);
PdfStamper stamper = PdfStamper.createSignature(reader, fout, '\0', null, true);
appearance = stamper.getSignatureAppearance();
appearance.setRenderingMode(PdfSignatureAppearance.RenderingMode.DESCRIPTION);          
Calendar currentDat = Calendar.getInstance();
currentDat.add(currentDat.MINUTE, 5);
appearance.setSignDate(currentDat);
appearance.setLayer2Text("Signed By");
appearance.setCertificationLevel(PdfSignatureAppearance.NOT_CERTIFIED);
appearance.setImage(null);
appearance.setVisibleSignature(rectangle, reader.getNumberOfPages(), null);
int contentEstimated = 8192;
HashMap<PdfName, Integer> exc = new HashMap();
exc.put(PdfName.CONTENTS, contentEstimated * 2 + 2);

PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED);
dic.setReason(appearance.getReason());
dic.setLocation(appearance.getLocation());
dic.setDate(new PdfDate(appearance.getSignDate()));

appearance.setCryptoDictionary(dic);

// Multiple Page Signature Annotations
/*PdfWriter pdfWriter = stamper.getWriter();
for (int p = 1; p <= reader.getNumberOfPages(); p++) {
    PdfFormField fieldSign = PdfFormField.createSignature(pdfWriter);
    fieldSign.setValue(dic);
    fieldSign.setFieldName("Test");
    fieldSign.setWidget(rectangle, PdfAnnotation.HIGHLIGHT_NONE);
    fieldSign.setFieldFlags(PdfAnnotation.FLAGS_READONLY);
    stamper.addAnnotation(fieldSign, p);
}*/
//End Annotations

appearance.preClose(exc);
BufferedReader resreader = new BufferedReader(new FileReader("certificate.xml"));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
String ls = System.getProperty("line.separator");
while ((line = resreader.readLine()) != null) {
    stringBuilder.append(line);
    stringBuilder.append(ls);
}

stringBuilder.deleteCharAt(stringBuilder.length() - 1);
resreader.close();
String response = stringBuilder.toString();
System.out.println("Response Signature:"+response);
String pkcsResponse = new XmlSigning().parseXml(response.trim());
System.out.println("DSA:"+pkcsResponse);
byte[] sigbytes = Base64.decodeBase64(pkcsResponse);
byte[] paddedSig = new byte[contentEstimated];
System.arraycopy(sigbytes, 0, paddedSig, 0, sigbytes.length);
PdfDictionary dic2 = new PdfDictionary();
dic2.put(PdfName.CONTENTS, new PdfString(paddedSig).setHexWriting(true));
appearance.close(dic2);

So kindly guide me to resolve this problem.

mkl
  • 90,588
  • 15
  • 125
  • 265
  • Whenever you sign a PDF interoperably, all of its pages are signed. So do you probably mean that you want to sign the PDF with one signature and get visualizations of the signature on each document page? Or do you mean something else entirely? – mkl Mar 03 '20 at 11:16
  • The last comment in this [link](https://stackoverflow.com/questions/35701597/how-to-show-digital-pdf-signature-in-all-documents-page-using-itext) might resolve your issue – Seshidhar G Mar 03 '20 at 11:17
  • @mkl Yes we want to visualise of the signature on each document page. – K A Pandian Mar 03 '20 at 11:51
  • @mkl Please help me to resolve this – K A Pandian Mar 03 '20 at 12:37
  • First of all, you surely know that having multiple appearances of the same signature is against the spirit or even the letter of the PDF specification. Nonetheless you can still create such signatures with multiple visualizations which Adobe Reader validates positively. If you're ok with actually patching iText, you may want to follow [this answer](https://stackoverflow.com/a/40757386/1729265). If you are not, you may want to port [this iTextSharp](https://stackoverflow.com/a/47762053/1729265) implementation. – mkl Mar 03 '20 at 16:24

0 Answers0