Rust Requires Chilkat v11.7.0+
Rust
Restrict Printing, Copying and Editing of a PDF
See more PDF Protection Examples
Demonstrates a PDF that opens without a password but limits what a reader may do with it. The user password is left empty, the owner password protects the restrictions, and the permissions member of the JSON says what is denied. Chilkat honors these permissions itself: with copying denied, text extraction is refused unless the PDF is loaded with its owner password. An empty owner password is refused when anything is restricted, because anyone opening the file would then have owner rights.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
let pdf = chilkat::Pdf::new();
if pdf.load_file("qa_data/pdf/two_page_memo.pdf").is_err() {
println!("{}", pdf.last_error_text());
return;
}
// No user password: the PDF opens without a prompt. The owner password is what protects the
// restrictions, so it must not be empty when anything is restricted.
pdf.set_user_password("");
pdf.set_owner_password("owner-secret");
let json = chilkat::JsonObject::new();
let _ = json.update_string("permissions.print", "lowQuality");
let _ = json.update_bool("permissions.copy", false);
let _ = json.update_bool("permissions.modify", false);
let _ = json.update_bool("permissions.annotate", false);
// The permissions not mentioned (fillForms, assemble, accessibility) stay granted.
let json_out = chilkat::JsonObject::new();
if pdf.protection("encrypt", &json, &json_out).is_err() {
println!("{}", pdf.last_error_text());
return;
}
if pdf.write_file("qa_output/memo_restricted.pdf").is_err() {
println!("{}", pdf.last_error_text());
return;
}
// Anyone can open the result, with the rights of a user.
let reader = chilkat::Pdf::new();
if reader.load_file("qa_output/memo_restricted.pdf").is_err() {
println!("{}", reader.last_error_text());
return;
}
let empty = chilkat::JsonObject::new();
let info = chilkat::JsonObject::new();
if reader.protection("getInfo", &empty, &info).is_err() {
println!("{}", reader.last_error_text());
return;
}
println!("opened with: {}", info.string_of("loaded.openedWith").unwrap_or_default());
println!("print: {}", info.string_of("loaded.permissions.print").unwrap_or_default());
println!("copy: {}", info.bool_of("loaded.permissions.copy"));
println!("modify: {}", info.bool_of("loaded.permissions.modify"));
// Chilkat honors the restrictions. Copying is not permitted, so the text cannot be extracted.
let opts = chilkat::JsonObject::new();
let extract_out = chilkat::JsonObject::new();
let sb = chilkat::StringBuilder::new();
if reader.extract_text_sb("text", &opts, &sb, &extract_out).is_err() {
println!("Text extraction was refused: the PDF does not permit copying.");
}
// The owner password lifts every restriction.
let owner = chilkat::Pdf::new();
owner.set_owner_password("owner-secret");
if owner.load_file("qa_output/memo_restricted.pdf").is_err() {
println!("{}", owner.last_error_text());
return;
}
let owner_info = chilkat::JsonObject::new();
if owner.protection("getInfo", &empty, &owner_info).is_err() {
println!("{}", owner.last_error_text());
return;
}
println!("opened with: {}", owner_info.string_of("loaded.openedWith").unwrap_or_default());
if owner.extract_text_sb("text", &opts, &sb, &extract_out).is_err() {
println!("{}", owner.last_error_text());
return;
}
println!("characters extracted with the owner password: {}", sb.length());
// Sample output:
// opened with: emptyPassword
// print: lowQuality
// copy: False
// modify: False
// Text extraction was refused: the PDF does not permit copying.
// opened with: owner
// characters extracted with the owner password: 1048