we want to sign the pdf with external signature provided by esign service.we have used openpdf 1.0.1..
the problem is we are unable to calculate the exclusion size of signature appearance before preclose..
please find below code -
byte[] signeddata = null;
PdfSignatureAppearance pdfSigApp=null;
File destFile=null;
PdfReader reader=null;
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
reader = new PdfReader(signingHelper.getSrc());
destFile = new File(signingHelper.getDest());
OutputStream os = arrayOutputStream;
PdfStamper pdfStamper = PdfStamper.createSignature(reader,os, '\0',null, true);
pdfSigApp = pdfStamper.getSignatureAppearance();
pdfSigApp.setVisibleSignature("SignatureField1[0]");// for existing
// signaure feild
SimpleDateFormat dt = new SimpleDateFormat("dd-MMM-yyyy");
String formatedDate = dt.format(new Date()); // pass name of
// field
pdfSigApp.setLayer2Text("Digitally Signed" + "\nReason: " + signingHelper.getReason()
+"\nDate: "+formatedDate+"\nLocation: " + signingHelper.getLocation());
Font font = new Font();
//font.setColor(Color);
font.setSize(9);
pdfSigApp.setLayer2Font(font);
pdfSigApp.setLocation(signingHelper.getLocation());
// pdfSigApp.set
PdfSignature sigDic = new PdfSignature(PdfName.ADOBE_PPKMS, PdfName.ADBE_PKCS7_DETACHED);
sigDic.put(PdfName.FT, PdfName.SIG);
sigDic.setReason(signingHelper.getReason());
sigDic.setLocation(signingHelper.getLocation());
pdfSigApp.setCryptoDictionary(sigDic);
------------------------------
HashMap exclusions = new HashMap();
exclusions.put(PdfName.CONTENTS,new Integer(7622)); //== Here is problem how to caclucate this Value ?
pdfSigApp.preClose(exclusions);
LOGGER.info("exclusions:"+exclusions);
String hashPdf = generateSha256HashInHexForPdf(pdfSigApp.getRangeStream());
//sending this hash to external service for sining----geting pkcs7 signature in response..
signeddata = Base64.getDecoder().decode(pkcs7Signature);
byte out[] = new byte[signeddata.length];
System.arraycopy(signeddata, 0, out, 0, signeddata.length);
updates.put(PdfName.CONTENTS, new PdfString(out).setHexWriting(true));
pdfSigApp.close(updates);
reader.close();
FileOutputStream fileOutputStream = new FileOutputStream(destFile);
fileOutputStream.write(arrayOutputStream.toByteArray());
fileOutputStream.close();
please provide suggestions or help in above code...