Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.7.0+

Check a PDF Text Replacement Before Applying It

See more PDF Text Replacement Examples

Demonstrates the "check" operation, which finds every placeholder and label and applies the font rule but changes nothing, and how to read its report: "ok" says whether "apply" would succeed, "replacements" lists what would be replaced, and "problems" lists what prevents it, each with a reason. Here the first check fails because a placeholder on the page was given no value; the value is added and the check passes, and only then is "apply" called.

Chilkat Rust Downloads

Rust

// 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_template.pdf

let pdf = chilkat::Pdf::new();
if pdf.load_file("qa_data/pdf/invoice_template.pdf").is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

// Values for most of the template's placeholders -- but not {{po}}, {{qty2}}, {{desc2}}, {{price2}} or {{amount2}}.
let json = chilkat::JsonObject::new();
let _ = json.update_string("placeholders.customer", "Acme Corp");
let _ = json.update_string("placeholders.date", "2026-10-18");
let _ = json.update_string("placeholders.qty1", "2");
let _ = json.update_string("placeholders.desc1", "Cordless drill, 18 V");
let _ = json.update_string("placeholders.price1", "$460.00");
let _ = json.update_string("placeholders.amount1", "$920.00");
let _ = json.update_string("placeholders.subtotal", "$920.00");
let _ = json.update_string("placeholders.taxRate", "7.35%");
let _ = json.update_string("placeholders.tax", "$67.62");
let _ = json.update_string("placeholders.total", "$987.62");

// "check" reports what "apply" would do, without changing the PDF.
let json_out = chilkat::JsonObject::new();
if pdf.text_replace("check", &json, &json_out).is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

println!("ok: {}", json_out.bool_of("ok"));
println!("would replace: {} placeholders", json_out.size_of_array("replacements"));

// Each problem names the placeholder (field) or label, and a reason:
//   notFound - the placeholder or label is not on the selected pages
//   unusedPlaceholder - a placeholder on the page was given no value
//   missingGlyph, notInEncoding, nonStandardUnembeddedFont, unsupportedFont - the font rule
//   placeholderSpansFonts - the text is drawn with more than one font or size
let mut i = 0;
let count_i = json_out.size_of_array("problems");
while i < count_i {
    json_out.set_i(i);
    println!("problem: {}{} - {} (page {})", json_out.string_of("problems[i].field").unwrap_or_default(), json_out.string_of("problems[i].label").unwrap_or_default(), json_out.string_of("problems[i].reason").unwrap_or_default(), json_out.int_of("problems[i].page"));
    i = i + 1;
}

// Give the missing placeholders their values and check again.
let _ = json.update_string("placeholders.po", "PO-88213");
let _ = json.update_string("placeholders.qty2", "0");
let _ = json.update_string("placeholders.desc2", "(no second item)");
let _ = json.update_string("placeholders.price2", "");
let _ = json.update_string("placeholders.amount2", "");
if pdf.text_replace("check", &json, &json_out).is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

println!("ok after adding the values: {}", json_out.bool_of("ok"));

// Now apply.
if json_out.bool_of("ok") {
    if pdf.text_replace("apply", &json, &json_out).is_err() {
        println!("{}", pdf.last_error_text());
        return;
    }

    println!("applied: {}", json_out.bool_of("applied"));
    if pdf.write_file("qa_output/invoice_checked.pdf").is_err() {
        println!("{}", pdf.last_error_text());
        return;
    }

}

// Sample output:

// ok: false
// would replace: 10 placeholders
// problem: po - unusedPlaceholder (page 1)
// problem: qty2 - unusedPlaceholder (page 1)
// problem: desc2 - unusedPlaceholder (page 1)
// problem: price2 - unusedPlaceholder (page 1)
// problem: amount2 - unusedPlaceholder (page 1)
// ok after adding the values: true
// applied: true