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.