Rust Requires Chilkat v11.7.0+
Rust
A PDF Label That Is Not on the Page: notFound or Ignore
See more PDF Text Replacement Examples
Demonstrates the "unmatchedValues" option. A label or placeholder given a value but not found on the selected pages is a problem with the reason notFound, which stops "apply", because a missing label usually means the wrong document or a typo in the label. With unmatchedValues "ignore" such a value is skipped and the rest is applied -- the right choice when one set of values serves several document layouts.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 invoice_labels.pdf
let pdf = chilkat::Pdf::new();
if pdf.load_file("qa_data/pdf/invoice_labels.pdf").is_err() {
println!("{}", pdf.last_error_text());
return;
}
let json = chilkat::JsonObject::new();
let json_out = chilkat::JsonObject::new();
// "Tracking Number:" is not on this invoice.
let _ = json.update_string("labels[0].label", "Invoice Number:");
let _ = json.update_string("labels[0].value", "INV-2026-0500");
let _ = json.update_string("labels[1].label", "Tracking Number:");
let _ = json.update_string("labels[1].value", "1Z999AA10123456784");
if pdf.text_replace("check", &json, &json_out).is_err() {
println!("{}", pdf.last_error_text());
return;
}
println!("default: ok = {}, problem: \"{}\" {}", json_out.bool_of("ok"), json_out.string_of("problems[0].label").unwrap_or_default(), json_out.string_of("problems[0].reason").unwrap_or_default());
// With "ignore", the label that is not there is skipped.
let _ = json.update_string("unmatchedValues", "ignore");
if pdf.text_replace("apply", &json, &json_out).is_err() {
println!("{}", pdf.last_error_text());
return;
}
println!("ignore: applied = {}, replacements: {}", json_out.bool_of("applied"), json_out.size_of_array("replacements"));
if pdf.write_file("qa_output/invoice_renumbered.pdf").is_err() {
println!("{}", pdf.last_error_text());
return;
}
// Sample output:
// default: ok = false, problem: "Tracking Number:" notFound
// ignore: applied = true, replacements: 1