Hi,
I’m trying to lock the pdf document and then to digitally sign it, When I do both things i get from pdfReader this message : Document has been altered or corrupted sience it was signed. But if i only sign document without locking, i get valid certificate.
This is my code for locking:
public ByteArrayOutputStream setLockDocument(Document doc, FileSecurity fileSecurity) {
DocumentPrivilege privilege = DocumentPrivilege.getAllowAll();
if (fileSecurity != null) {
if (fileSecurity.isLockEdit()) {
privilege.setAllowModifyContents(false);
privilege.setAllowAssembly(false);
// privilege.setChangeAllowLevel(0);
}
if (fileSecurity.isLockPrint()) {
privilege.setAllowPrint(false);
privilege.setAllowDegradedPrinting(false);
privilege.setPrintAllowLevel(0);
}
if (fileSecurity.isLockExtract()) {
privilege.setAllowCopy(false);
privilege.setAllowScreenReaders(false);
privilege.setCopyAllowLevel(0);
}
if (fileSecurity.isLockStamp()) {
privilege.setAllowModifyAnnotations(false);
privilege.setAllowFillIn(false);
}
}
//Set the desired privileges
PdfFileSecurity pdfFileSecurity = null;
pdfFileSecurity = new PdfFileSecurity(doc);
pdfFileSecurity.setPrivilege(privilege);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pdfFileSecurity.save(baos);
pdfFileSecurity.close();
doc.close();
return baos;
and signing:
public ByteArrayOutputStream signDocument(Document doc, String signedBy) throws Exception {
PdfFileSignature pdfSignSingle = new PdfFileSignature();
pdfSignSingle.bindPdf(doc);
pdfSignSingle.setCertificate(prop.getSigningKeyStorePath(), prop.getKeystorePassword());
PKCS7 signature = new PKCS7(prop.getSigningKeyStorePath(), prop.getKeystorePassword());
pdfSignSingle.setSignatureAppearance(prop.getSimploudLogo());
signature.setAuthority("Authority");
signature.setDate(new Date());
signature.setContactInfo("email");
signature.setLocation("location");
signature.setReason("reason");
pdfSignSingle.sign(1, true, new java.awt.Rectangle(100, 100, 200, 200), signature);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pdfSignSingle.save(baos);
pdfSignSingle.close();
doc.close();
return baos;
}