Java Requires Chilkat v11.7.0+
Chilkat2 Java
Encrypt and Sign a PDF, and Sign an Encrypted PDF
See more PDF Protection Examples
Demonstrates signing together with encryption. A pending encrypt is applied by SignPdf before it signs, so one call produces a PDF that is both encrypted and signed. A PDF that is already encrypted can be signed too, and stays encrypted. When it was opened with only the user password its permissions decide: adding a signature field needs both the modify and the annotate permission. Loading the PDF with its owner password lifts that.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Pdf;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The PDF used in this example is available at two_page_memo.pdf
// The signing certificate, with its private key.
Cert cert = new Cert();
try {
cert.loadPfxFile("qa_data/pfx/myPdfSigningCert.pfx", "secret");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
Pdf pdf = new Pdf();
try {
pdf.loadFile("qa_data/pdf/two_page_memo.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Encrypt with AES-256 and do not permit modifying.
pdf.setUserPassword("user-secret");
pdf.setOwnerPassword("owner-secret");
JsonObject json = new JsonObject();
json.updateBool("permissions.modify", false);
JsonObject jsonOut = new JsonObject();
try {
pdf.protection("encrypt", json, jsonOut);
// The encryption is still pending. SignPdf applies it first and signs the encrypted result.
pdf.setSigningCert(cert);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonObject sigOpts = new JsonObject();
sigOpts.updateBool("invisibleSignature", true);
sigOpts.updateInt("signingCertificateV2", 1);
sigOpts.updateInt("signingTime", 1);
try {
pdf.signPdf(sigOpts, "qa_output/memo_encrypted_signed.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// The result is encrypted and signed.
Pdf check = new Pdf();
check.setUserPassword("user-secret");
try {
check.loadFile("qa_output/memo_encrypted_signed.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonObject empty = new JsonObject();
JsonObject info = new JsonObject();
try {
check.protection("getInfo", empty, info);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("algorithm: " + info.stringOf("loaded.algorithm"));
System.out.println("signatures: " + check.getNumSignatures());
JsonObject sigInfo = new JsonObject();
boolean verified;
try {
check.verifySignature(0, sigInfo);
verified = true;
} catch (ChilkatException e) {
verified = false;
}
System.out.println("signature verifies: " + verified);
// Signing an encrypted PDF. This one was opened with only the user password, and its
// permissions do not allow modifying, so another signature is refused.
try {
check.setSigningCert(cert);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
try {
check.signPdf(sigOpts, "qa_output/memo_signed_twice.pdf");
} catch (ChilkatException e) {
System.out.println("A second signature was refused: the permissions do not allow it.");
}
// Loaded with the owner password, the same PDF can be signed. It stays encrypted, and the
// first signature stays valid because the new one is added as an incremental update.
Pdf owner = new Pdf();
owner.setUserPassword("user-secret");
owner.setOwnerPassword("owner-secret");
try {
owner.loadFile("qa_output/memo_encrypted_signed.pdf");
owner.setSigningCert(cert);
owner.signPdf(sigOpts, "qa_output/memo_signed_twice.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
Pdf twice = new Pdf();
twice.setUserPassword("user-secret");
try {
twice.loadFile("qa_output/memo_signed_twice.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("signatures after signing with owner rights: " + twice.getNumSignatures());
try {
twice.verifySignature(0, sigInfo);
verified = true;
} catch (ChilkatException e) {
verified = false;
}
System.out.println("first signature still verifies: " + verified);
try {
twice.verifySignature(1, sigInfo);
verified = true;
} catch (ChilkatException e) {
verified = false;
}
System.out.println("second signature verifies: " + verified);
// Sample output:
// algorithm: aes-256
// signatures: 1
// signature verifies: True
// A second signature was refused: the permissions do not allow it.
// signatures after signing with owner rights: 2
// first signature still verifies: True
// second signature verifies: True
}
}