2) When I check the signature validity in Adobe Acrobat DC, it says signature is invalid.
3) I tried to verify the signature of the signed PDF using "signature.verifySignature()", it throws error saying - '(Signature name) failed to verified'
4) The sample code we used for signing is shown below:
- Code: Select all
X509Certificate x509Cert = (X509Certificate) DSign.keyStore.getCertificate(requestAlias); // getting the certificate data from the USB token using signature alias
PrivateKey privateKey = (PrivateKey) DSign.keyStore.getKey(requestAlias,
privateKeyPassword != null ? privateKeyPassword.toCharArray() : null);
File inputTempFile = File.createTempFile(fileName, ".pdf");
byte[] decoded = Base64.getDecoder().decode(requestPdfData.getBytes()); // requestPdfData is the pdf file got using base64 format
FileOutputStream fos = new FileOutputStream(inputTempFile);
fos.write(decoded);
fos.close();
String inputPdfPath = inputTempFile.getAbsolutePath();
String outputPdfPath = inputPdfPath.replace(".pdf", "_signed.pdf");
PdfDocument doc = new PdfDocument();
doc.loadFromFile(inputPdfPath);
PdfCertificate pdfCert = new PdfCertificate(x509Cert.getEncoded()); // Certificate data is got using the USB token which is encoded
//Create a PdfSignature object and specify its position and size
PdfSignature signature = new PdfSignature(doc, doc.getPages().get(0), pdfCert, "My signature");
Rectangle2D rect = new Rectangle2D.Float();
rect.setFrame(new Point2D.Float((float) doc.getPages().get(0).getActualSize().getWidth() - 250, (float) doc.getPages().get(doc.getPages().getCount() - 1).getActualSize().getHeight() - signLocation), new Dimension(250, 100));
signature.setBounds(rect);
//Set the graphics mode
signature.setGraphicMode(GraphicMode.Sign_Detail);
// signature.removeShowConfigureText(SignatureConfigureText.Distinguished_Name);
// signature.setName(partyName);
signature.setDateLabel("Date:");
signature.setDate(new java.util.Date());
signature.setReasonLabel("Reason: ");
signature.setReason("Online Approval\n");
// signature.setSignatureName("My Signature");
//Set the signature font
// signature.setSignInfoFont(new PdfFont(PdfFontFamily.Courier, 11f, PdfFontStyle.Regular));
signature.setSignDetailsFont(new PdfFont(PdfFontFamily.Helvetica, 10f, PdfFontStyle.Bold));
// signature.setSignNameFont(new PdfFont(PdfFontFamily.Helvetica, 10f, PdfFontStyle.Bold));
//Set the document permission
signature.setDocumentPermissions(PdfCertificationFlags.Forbid_Changes);
signature.setCertificated(true);
//Save to file
doc.saveToFile(outputPdfPath);
doc.close();
File outputFile = new File(outputPdfPath);
byte[] fileContent = Files.readAllBytes(outputFile.toPath());
signedPdfData = Base64.getEncoder().encodeToString(fileContent);