Rust Requires Chilkat v11.7.0+
Rust
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 Rust Downloads
// 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.
let recipient = chilkat::Cert::new();
if recipient.load_from_file("qa_data/certs/recipient.cer").is_err() {
println!("{}", recipient.last_error_text());
return;
}
let pdf = chilkat::Pdf::new();
if pdf.load_file("qa_data/pdf/two_page_memo.pdf").is_err() {
println!("{}", pdf.last_error_text());
return;
}
// Call AddEncryptCert once for each recipient.
if pdf.add_encrypt_cert(&recipient).is_err() {
println!("{}", pdf.last_error_text());
return;
}
// recipientPermissions[0] belongs to the first certificate added. changeSecurity gives that
// recipient owner rights, so the file can be managed later.
let json = chilkat::JsonObject::new();
let _ = json.update_string("method", "certificate");
let _ = json.update_bool("recipientPermissions[0].changeSecurity", true);
let json_out = chilkat::JsonObject::new();
if pdf.protection("encrypt", &json, &json_out).is_err() {
println!("{}", pdf.last_error_text());
return;
}
println!("method: {}", json_out.string_of("pending.method").unwrap_or_default());
println!("algorithm: {}", json_out.string_of("pending.algorithm").unwrap_or_default());
println!("recipients: {}", json_out.size_of_array("pending.recipients"));
if pdf.write_file("qa_output/memo_for_recipient.pdf").is_err() {
println!("{}", pdf.last_error_text());
return;
}
// PreLoadCheck says how the file is protected, without loading it.
let reader = chilkat::Pdf::new();
let info = chilkat::JsonObject::new();
if reader.pre_load_check("qa_output/memo_for_recipient.pdf", &info).is_err() {
println!("{}", reader.last_error_text());
return;
}
println!("saved file, method: {}", info.string_of("method").unwrap_or_default());
println!("saved file, recipients: {}", info.size_of_array("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.)
let my_cert = chilkat::Cert::new();
if my_cert.load_pfx_file("qa_data/pfx/recipient.pfx", "pfx-password").is_err() {
println!("{}", my_cert.last_error_text());
return;
}
if reader.set_decrypt_cert(&my_cert).is_err() {
println!("{}", reader.last_error_text());
return;
}
if reader.load_file("qa_output/memo_for_recipient.pdf").is_err() {
println!("{}", reader.last_error_text());
return;
}
let empty = chilkat::JsonObject::new();
let loaded_info = chilkat::JsonObject::new();
if reader.protection("getInfo", &empty, &loaded_info).is_err() {
println!("{}", reader.last_error_text());
return;
}
println!("opened with: {}", loaded_info.string_of("loaded.openedWith").unwrap_or_default());
println!("pages: {}", reader.num_pages());
// Sample output:
// method: certificate
// algorithm: aes-256
// recipients: 1
// saved file, method: certificate
// saved file, recipients: 1
// opened with: certificate
// pages: 2