Rust Requires Chilkat v11.7.0+
Rust
PDF Placeholders Without a Value: Error, Blank or Keep
See more PDF Text Replacement Examples
Demonstrates the "unusedPlaceholders" option, which decides what happens to a placeholder found on the page that has no value in "placeholders": "error" (the default) makes it a problem with the reason unusedPlaceholder, so nothing is applied; "blank" replaces it with nothing; "keep" leaves it on the page as it is.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_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 every placeholder except {{po}} (the purchase order number, which this customer has none of).
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.qty2", "6");
let _ = json.update_string("placeholders.desc2", "Galvanized hinge, 4 inch");
let _ = json.update_string("placeholders.price2", "$12.50");
let _ = json.update_string("placeholders.amount2", "$75.00");
let _ = json.update_string("placeholders.subtotal", "$995.00");
let _ = json.update_string("placeholders.taxRate", "7.35%");
let _ = json.update_string("placeholders.tax", "$73.13");
let _ = json.update_string("placeholders.total", "$1,068.13");
let json_out = chilkat::JsonObject::new();
// The default, "error": the unfilled {{po}} is a problem and nothing would be applied.
if pdf.text_replace("check", &json, &json_out).is_err() {
println!("{}", pdf.last_error_text());
return;
}
println!("error (default): ok = {}, problem: {} {}", json_out.bool_of("ok"), json_out.string_of("problems[0].field").unwrap_or_default(), json_out.string_of("problems[0].reason").unwrap_or_default());
// "keep": {{po}} stays on the page, literally.
let _ = json.update_string("unusedPlaceholders", "keep");
if pdf.text_replace("check", &json, &json_out).is_err() {
println!("{}", pdf.last_error_text());
return;
}
println!("keep: ok = {}", json_out.bool_of("ok"));
// "blank": {{po}} is replaced by nothing, leaving "PO Number:" with an empty value.
let _ = json.update_string("unusedPlaceholders", "blank");
if pdf.text_replace("apply", &json, &json_out).is_err() {
println!("{}", pdf.last_error_text());
return;
}
println!("blank: applied = {}", json_out.bool_of("applied"));
if pdf.write_file("qa_output/invoice_no_po.pdf").is_err() {
println!("{}", pdf.last_error_text());
return;
}
// Sample output:
// error (default): ok = false, problem: po unusedPlaceholder
// keep: ok = true
// blank: applied = true