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

What PDF Text Replacement Does Not Change: Metadata, Links, Form Fields

See more PDF Text Replacement Examples

TextReplace changes the page content only. A placeholder that also appears in the document's metadata (the Title, Author, Subject, Keywords, Creator or Producer), in a link's target URL, or in a form field's value is left as it is and reported in "notReplaced" with "where" saying which. The invoice template's title is "Invoice for {{customer}}" and its "View this invoice online" link points at a URL that holds {{customer}}.

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;
}

let json = chilkat::JsonObject::new();
let _ = json.update_string("placeholders.customer", "Acme Corp");
// (The other placeholders are left as they are for this example.)
let _ = json.update_string("unusedPlaceholders", "keep");

let json_out = chilkat::JsonObject::new();
if pdf.text_replace("apply", &json, &json_out).is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

println!("In the page content, customer was replaced {} times.", json_out.int_of("replacements[0].count"));

// Where else the placeholder occurs, untouched:
let mut i = 0;
let count_i = json_out.size_of_array("notReplaced");
while i < count_i {
    json_out.set_i(i);
    println!("not replaced: {{{{{}}}}} in {}", json_out.string_of("notReplaced[i].field").unwrap_or_default(), json_out.string_of("notReplaced[i].where").unwrap_or_default());
    i = i + 1;
}

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

// Sample output:

// In the page content, customer was replaced 2 times.
// not replaced: {{customer}} in metadata
// not replaced: {{customer}} in linkTarget