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

Replace the Value After a Label in a PDF (Re-issue an Invoice)

See more PDF Text Replacement Examples

Demonstrates replacing the values that follow labels such as "Invoice Number:" on a PDF that has no placeholders -- an ordinary invoice. Each entry of the "labels" array names a label as it appears on the page and the new value; the value replaces the rest of the label's line. Label matching ignores case, extra spaces and a trailing colon. The replaced text is read back with ExtractText to confirm it.

Chilkat Rust Downloads

Rust
let mut success = false;

// 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();
success = pdf.load_file("qa_data/pdf/invoice_labels.pdf").is_ok();
if !success {
    println!("{}", pdf.last_error_text());
    return;
}

// The invoice has lines such as "Invoice Number:   INV-2026-0412" and "Total Due:  $1,234.53".
// Each label's new value replaces whatever follows the label on its line.
let json = chilkat::JsonObject::new();
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", "Invoice Date:");
let _ = json.update_string("labels[1].value", "April 2, 2026");
let _ = json.update_string("labels[2].label", "customer");
let _ = json.update_string("labels[2].value", "Contoso Ltd");
let _ = json.update_string("labels[3].label", "Total Due:");
let _ = json.update_string("labels[3].value", "$1,999.99");

let json_out = chilkat::JsonObject::new();
success = pdf.text_replace("apply", &json, &json_out).is_ok();
if !success {
    println!("Not applied: {}", json_out.emit().unwrap_or_default());
    if !success {
        println!("{}", pdf.last_error_text());
        return;
    }

}

let mut i = 0;
let count_i = json_out.size_of_array("replacements");
while i < count_i {
    json_out.set_i(i);
    println!("Replaced the value of \"{}\" on page {}", json_out.string_of("replacements[i].label").unwrap_or_default(), json_out.string_of("replacements[i].pages").unwrap_or_default());
    i = i + 1;
}

if pdf.write_file("qa_output/invoice_reissued.pdf").is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

// Read the new values back with ExtractText's label option.
let json_read = chilkat::JsonObject::new();
let json_text = chilkat::JsonObject::new();
let _ = json_read.update_string("label", "Total Due");
let _ = json_read.update_string("labelValue", "right");
if pdf.extract_text("text", &json_read, &json_text).is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

println!("Total Due is now: {}", json_text.string_of("text").unwrap_or_default());

// Sample output:

// Replaced the value of "Invoice Number:" on page 1
// Replaced the value of "Invoice Date:" on page 1
// Replaced the value of "customer" on page 1
// Replaced the value of "Total Due:" on page 1
// Total Due is now: $1,999.99