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

Why a PDF Text Replacement Is Refused: the Font Rule

See more PDF Text Replacement Examples

A PDF stores glyphs drawn with particular fonts, so new text can only be drawn when the original font can draw it. This example shows the two common refusals. A font embedded as a subset carries only the glyphs the document used: a value made of other characters is refused with the reason missingGlyph, and the report names the font and the characters it lacks. A font that is neither one of the standard 14 nor embedded (here Verdana) is refused with nonStandardUnembeddedFont, because Chilkat cannot know what a viewer will draw for it. A non-embedded Arial, Times New Roman or Courier New is treated as the standard font every viewer substitutes for 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 quote_subset_font.pdf
// and letter_verdana.pdf

// 1. An embedded subset font: only the glyphs the document uses are in the PDF.
let pdf = chilkat::Pdf::new();
success = pdf.load_file("qa_data/pdf/quote_subset_font.pdf").is_ok();
if !success {
    println!("{}", pdf.last_error_text());
    return;
}

let json = chilkat::JsonObject::new();
let json_out = chilkat::JsonObject::new();

// The regular face's subset carries the characters of the quotation's text, among them the digits of
// "58341".  A new number made of those digits can be drawn...
let _ = json.update_string("labels[0].label", "Quote #:");
let _ = json.update_string("labels[0].value", "58314");
success = pdf.text_replace("check", &json, &json_out).is_ok();
if !success {
    println!("{}", pdf.last_error_text());
    return;
}

println!("58314: ok = {}", json_out.bool_of("ok"));

// ...but "Q" and "-" are not in the subset (nothing in the quotation's regular text uses them).
let _ = json.update_string("labels[0].value", "Q-58341");
success = pdf.text_replace("check", &json, &json_out).is_ok();
if !success {
    println!("{}", pdf.last_error_text());
    return;
}

println!("Q-58341: ok = {}", json_out.bool_of("ok"));
if !json_out.bool_of("ok") {
    // The problem names the font and the characters it cannot draw.
    println!("    reason: {}", json_out.string_of("problems[0].reason").unwrap_or_default());
    println!("    font: {}", json_out.string_of("problems[0].font").unwrap_or_default());
    println!("    missing: {}", json_out.string_of("problems[0].chars").unwrap_or_default());
}

// "apply" with a problem returns false and changes nothing.
success = pdf.text_replace("apply", &json, &json_out).is_ok();
println!("apply returned {}, applied = {}", success, json_out.bool_of("applied"));

// 2. A font that is not embedded and not one of the standard 14.
let pdf2 = chilkat::Pdf::new();
if pdf2.load_file("qa_data/pdf/letter_verdana.pdf").is_err() {
    println!("{}", pdf2.last_error_text());
    return;
}

let json2 = chilkat::JsonObject::new();
let _ = json2.update_string("labels[0].label", "Account Number:");
let _ = json2.update_string("labels[0].value", "0000099999");
if pdf2.text_replace("check", &json2, &json_out).is_err() {
    println!("{}", pdf2.last_error_text());
    return;
}

println!("Verdana, not embedded: ok = {}, reason: {}, font: {}", json_out.bool_of("ok"), json_out.string_of("problems[0].reason").unwrap_or_default(), json_out.string_of("problems[0].font").unwrap_or_default());

// Sample output:

// 58314: ok = true
// Q-58341: ok = false
//     reason: missingGlyph
//     font: AAAAAA+DejaVuSans
//     missing: Q-
// apply returned false, applied = false
// Verdana, not embedded: ok = false, reason: nonStandardUnembeddedFont, font: Verdana