Java Requires Chilkat v11.7.0+
Chilkat2 Java
Encrypt a PDF for a Certificate Instead of a Password
See more PDF Protection Examples
Demonstrates certificate security, where a PDF is encrypted for one or more recipients and each opens it with their own certificate and private key. No password is involved. Only the public certificate of a recipient is needed to encrypt. Each recipient has its own permissions, and the changeSecurity permission gives a recipient owner rights. PreLoadCheck tells an application that a file is certificate-encrypted and which certificates can open it. A PDF that cannot be opened, because no matching certificate and private key were provided, fails to load.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 recipient's certificate. Only the public certificate is needed to encrypt.
Cert recipient = new Cert();
try {
recipient.loadFromFile("qa_data/certs/recipient.cer");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
Pdf pdf = new Pdf();
try {
pdf.loadFile("qa_data/pdf/two_page_memo.pdf");
// Call AddEncryptCert once for each recipient.
pdf.addEncryptCert(recipient);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// recipientPermissions[0] belongs to the first certificate added. changeSecurity gives that
// recipient owner rights, so the file can be managed later.
JsonObject json = new JsonObject();
json.updateString("method", "certificate");
json.updateBool("recipientPermissions[0].changeSecurity", true);
JsonObject jsonOut = new JsonObject();
try {
pdf.protection("encrypt", json, jsonOut);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("method: " + jsonOut.stringOf("pending.method"));
System.out.println("algorithm: " + jsonOut.stringOf("pending.algorithm"));
System.out.println("recipients: " + jsonOut.sizeOfArray("pending.recipients"));
try {
pdf.writeFile("qa_output/memo_for_recipient.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// PreLoadCheck says how the file is protected, without loading it.
Pdf reader = new Pdf();
JsonObject info = new JsonObject();
try {
reader.preLoadCheck("qa_output/memo_for_recipient.pdf", info);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("saved file, method: " + info.stringOf("method"));
System.out.println("saved file, recipients: " + info.sizeOfArray("recipients"));
// The recipient opens it with the certificate and its private key, here from a .pfx file.
// (On Windows, a certificate that is installed in the Windows certificate store with its private
// key is found automatically when the PDF is loaded, and SetDecryptCert is not needed for it.)
Cert myCert = new Cert();
try {
myCert.loadPfxFile("qa_data/pfx/recipient.pfx", "pfx-password");
reader.setDecryptCert(myCert);
reader.loadFile("qa_output/memo_for_recipient.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonObject empty = new JsonObject();
JsonObject loadedInfo = new JsonObject();
try {
reader.protection("getInfo", empty, loadedInfo);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("opened with: " + loadedInfo.stringOf("loaded.openedWith"));
System.out.println("pages: " + reader.getNumPages());
// Sample output:
// method: certificate
// algorithm: aes-256
// recipients: 1
// saved file, method: certificate
// saved file, recipients: 1
// opened with: certificate
// pages: 2
}
}