Rust Requires Chilkat v11.7.0+
Rust
Replace PDF Text on Selected Pages Only
See more PDF Text Replacement Examples
Demonstrates the "pages" option, which limits the search for placeholders and labels to the given pages: "all" (the default), "first", "last", "odd", "even", or a list such as "1,3,5-9". The two-page order confirmation carries the same "Status:" label on both pages; the status is changed on page 2 only, then on both pages.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 order_confirmation.pdf
let pdf = chilkat::Pdf::new();
if pdf.load_file("qa_data/pdf/order_confirmation.pdf").is_err() {
println!("{}", pdf.last_error_text());
return;
}
let json = chilkat::JsonObject::new();
let json_out = chilkat::JsonObject::new();
// Both pages have "Status: Pending". With pages "2", only page 2 is searched.
let _ = json.update_string("pages", "2");
let _ = json.update_string("labels[0].label", "Status:");
let _ = json.update_string("labels[0].value", "Shipped");
if pdf.text_replace("check", &json, &json_out).is_err() {
println!("{}", pdf.last_error_text());
return;
}
println!("pages 2: found on page(s) {}", json_out.string_of("replacements[0].pages").unwrap_or_default());
// With every page searched, the default occurrence ("first") still replaces one -- the first in reading order.
let _ = json.update_string("pages", "all");
if pdf.text_replace("check", &json, &json_out).is_err() {
println!("{}", pdf.last_error_text());
return;
}
println!("pages all, first occurrence: found on page(s) {}", json_out.string_of("replacements[0].pages").unwrap_or_default());
// Every occurrence on every page.
let _ = json.update_string("labels[0].occurrence", "all");
if pdf.text_replace("apply", &json, &json_out).is_err() {
println!("{}", pdf.last_error_text());
return;
}
println!("pages all, every occurrence: {} replaced on page(s) {}", json_out.int_of("replacements[0].count"), json_out.string_of("replacements[0].pages").unwrap_or_default());
if pdf.write_file("qa_output/order_shipped.pdf").is_err() {
println!("{}", pdf.last_error_text());
return;
}
// Sample output:
// pages 2: found on page(s) 2
// pages all, first occurrence: found on page(s) 1
// pages all, every occurrence: 2 replaced on page(s) 1,2