Rust Requires Chilkat v11.7.0+
Rust
Remove the Password and Encryption from a PDF
See more PDF Protection Examples
Demonstrates the removeEncryption operation, which saves the PDF with no encryption at all: no password, no restrictions. It needs owner rights, so the PDF is loaded with its owner password. As with every Protection operation, the change is applied when the file is written.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 encrypted_user_password.pdf
let pdf = chilkat::Pdf::new();
// Removing the encryption needs owner rights: load with the owner password.
pdf.set_owner_password("owner-secret");
if pdf.load_file("qa_data/pdf/encrypted_user_password.pdf").is_err() {
println!("{}", pdf.last_error_text());
return;
}
let json = chilkat::JsonObject::new();
let json_out = chilkat::JsonObject::new();
if pdf.protection("getInfo", &json, &json_out).is_err() {
println!("{}", pdf.last_error_text());
return;
}
println!("encrypted, as loaded: {}", json_out.bool_of("loaded.encrypted"));
println!("algorithm: {}", json_out.string_of("loaded.algorithm").unwrap_or_default());
if pdf.protection("removeEncryption", &json, &json_out).is_err() {
println!("{}", pdf.last_error_text());
return;
}
println!("will be saved encrypted: {}", json_out.bool_of("pending.encrypted"));
if pdf.write_file("qa_output/minutes_unprotected.pdf").is_err() {
println!("{}", pdf.last_error_text());
return;
}
// The result is an ordinary PDF that opens without a password.
let reader = chilkat::Pdf::new();
let info = chilkat::JsonObject::new();
if reader.pre_load_check("qa_output/minutes_unprotected.pdf", &info).is_err() {
println!("{}", reader.last_error_text());
return;
}
println!("encrypted: {}", info.bool_of("encrypted"));
if reader.load_file("qa_output/minutes_unprotected.pdf").is_err() {
println!("{}", reader.last_error_text());
return;
}
println!("pages: {}", reader.num_pages());
// Sample output:
// encrypted, as loaded: True
// algorithm: rc4-128
// will be saved encrypted: False
// encrypted: False
// pages: 1