Rust Requires Chilkat v11.7.0+
Rust
Find Out Whether a PDF Needs a Password Before Loading It
See more PDF Protection Examples
Demonstrates PreLoadCheck, which examines a PDF file without loading it and without any password. It reports whether the file is encrypted, by which method and algorithm, whether a password is needed to open it, and what its permissions are. An application uses it to decide whether to ask for a password, or which certificate it needs, before calling LoadFile. PreLoadCheckBd does the same for a PDF held in memory.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 PDF used in this example is available at encrypted_user_password.pdf
// The PDF used in this example is available at no_copy_permission.pdf
let pdf = chilkat::Pdf::new();
let info = chilkat::JsonObject::new();
// A PDF that is not encrypted. Nothing but "encrypted" is reported for it.
if pdf.pre_load_check("qa_data/pdf/two_page_memo.pdf", &info).is_err() {
println!("{}", pdf.last_error_text());
return;
}
println!("two_page_memo.pdf");
println!(" encrypted: {}", info.bool_of("encrypted"));
// A PDF that needs a password to open.
if pdf.pre_load_check("qa_data/pdf/encrypted_user_password.pdf", &info).is_err() {
println!("{}", pdf.last_error_text());
return;
}
println!("encrypted_user_password.pdf");
println!(" encrypted: {}", info.bool_of("encrypted"));
println!(" method: {}", info.string_of("method").unwrap_or_default());
println!(" algorithm: {}", info.string_of("algorithm").unwrap_or_default());
println!(" openPasswordRequired: {}", info.bool_of("openPasswordRequired"));
// A PDF that opens without a password but is restricted. The permissions are readable without
// any password.
if pdf.pre_load_check("qa_data/pdf/no_copy_permission.pdf", &info).is_err() {
println!("{}", pdf.last_error_text());
return;
}
println!("no_copy_permission.pdf");
println!(" encrypted: {}", info.bool_of("encrypted"));
println!(" openPasswordRequired: {}", info.bool_of("openPasswordRequired"));
println!(" may copy: {}", info.bool_of("permissions.copy"));
// The typical use: ask for a password only when one is needed, then load.
if pdf.pre_load_check("qa_data/pdf/encrypted_user_password.pdf", &info).is_err() {
println!("{}", pdf.last_error_text());
return;
}
if info.bool_of("openPasswordRequired") {
// An application would prompt for the password here.
pdf.set_user_password("secret");
}
if pdf.load_file("qa_data/pdf/encrypted_user_password.pdf").is_err() {
println!("{}", pdf.last_error_text());
return;
}
println!("loaded, pages: {}", pdf.num_pages());
// Sample output:
// two_page_memo.pdf
// encrypted: False
// encrypted_user_password.pdf
// encrypted: True
// method: password
// algorithm: rc4-128
// openPasswordRequired: True
// no_copy_permission.pdf
// encrypted: True
// openPasswordRequired: False
// may copy: False
// loaded, pages: 1