Rust Requires Chilkat v11.7.0+
Rust
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 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 signing certificate, with its private key.
let cert = chilkat::Cert::new();
if cert.load_pfx_file("qa_data/pfx/myPdfSigningCert.pfx", "secret").is_err() {
println!("{}", cert.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;
}
// Encrypt with AES-256 and do not permit modifying.
pdf.set_user_password("user-secret");
pdf.set_owner_password("owner-secret");
let json = chilkat::JsonObject::new();
let _ = json.update_bool("permissions.modify", false);
let json_out = chilkat::JsonObject::new();
if pdf.protection("encrypt", &json, &json_out).is_err() {
println!("{}", pdf.last_error_text());
return;
}
// The encryption is still pending. SignPdf applies it first and signs the encrypted result.
if pdf.set_signing_cert(&cert).is_err() {
println!("{}", pdf.last_error_text());
return;
}
let sig_opts = chilkat::JsonObject::new();
let _ = sig_opts.update_bool("invisibleSignature", true);
let _ = sig_opts.update_int("signingCertificateV2", 1);
let _ = sig_opts.update_int("signingTime", 1);
if pdf.sign_pdf(&sig_opts, "qa_output/memo_encrypted_signed.pdf").is_err() {
println!("{}", pdf.last_error_text());
return;
}
// The result is encrypted and signed.
let check = chilkat::Pdf::new();
check.set_user_password("user-secret");
if check.load_file("qa_output/memo_encrypted_signed.pdf").is_err() {
println!("{}", check.last_error_text());
return;
}
let empty = chilkat::JsonObject::new();
let info = chilkat::JsonObject::new();
if check.protection("getInfo", &empty, &info).is_err() {
println!("{}", check.last_error_text());
return;
}
println!("algorithm: {}", info.string_of("loaded.algorithm").unwrap_or_default());
println!("signatures: {}", check.num_signatures());
let sig_info = chilkat::JsonObject::new();
let mut verified = check.verify_signature(0, &sig_info).is_ok();
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.
if check.set_signing_cert(&cert).is_err() {
println!("{}", check.last_error_text());
return;
}
if check.sign_pdf(&sig_opts, "qa_output/memo_signed_twice.pdf").is_err() {
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.
let owner = chilkat::Pdf::new();
owner.set_user_password("user-secret");
owner.set_owner_password("owner-secret");
if owner.load_file("qa_output/memo_encrypted_signed.pdf").is_err() {
println!("{}", owner.last_error_text());
return;
}
if owner.set_signing_cert(&cert).is_err() {
println!("{}", owner.last_error_text());
return;
}
if owner.sign_pdf(&sig_opts, "qa_output/memo_signed_twice.pdf").is_err() {
println!("{}", owner.last_error_text());
return;
}
let twice = chilkat::Pdf::new();
twice.set_user_password("user-secret");
if twice.load_file("qa_output/memo_signed_twice.pdf").is_err() {
println!("{}", twice.last_error_text());
return;
}
println!("signatures after signing with owner rights: {}", twice.num_signatures());
verified = twice.verify_signature(0, &sig_info).is_ok();
println!("first signature still verifies: {}", verified);
verified = twice.verify_signature(1, &sig_info).is_ok();
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