Sample code for 30+ languages & platforms
Tcl 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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  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.
set pdf [new_CkPdf]

set success [CkPdf_LoadFile $pdf "qa_data/pdf/quote_subset_font.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    exit
}

set json [new_CkJsonObject]

set jsonOut [new_CkJsonObject]

#  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...
CkJsonObject_UpdateString $json "labels[0].label" "Quote #:"
CkJsonObject_UpdateString $json "labels[0].value" "58314"
set success [CkPdf_TextReplace $pdf "check" $json $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    exit
}

puts "58314: ok = [CkJsonObject_BoolOf $jsonOut ok]"

#  ...but "Q" and "-" are not in the subset (nothing in the quotation's regular text uses them).
CkJsonObject_UpdateString $json "labels[0].value" "Q-58341"
set success [CkPdf_TextReplace $pdf "check" $json $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    exit
}

puts "Q-58341: ok = [CkJsonObject_BoolOf $jsonOut ok]"
if {[CkJsonObject_BoolOf $jsonOut "ok"] == 0} then {
    #  The problem names the font and the characters it cannot draw.
    puts "    reason: [CkJsonObject_stringOf $jsonOut {problems[0].reason}]"
    puts "    font: [CkJsonObject_stringOf $jsonOut {problems[0].font}]"
    puts "    missing: [CkJsonObject_stringOf $jsonOut {problems[0].chars}]"
}

#  "apply" with a problem returns false and changes nothing.
set success [CkPdf_TextReplace $pdf "apply" $json $jsonOut]
puts "apply returned $success, applied = [CkJsonObject_BoolOf $jsonOut applied]"

#  2. A font that is not embedded and not one of the standard 14.
set pdf2 [new_CkPdf]

set success [CkPdf_LoadFile $pdf2 "qa_data/pdf/letter_verdana.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf2]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkPdf $pdf2
    exit
}

set json2 [new_CkJsonObject]

CkJsonObject_UpdateString $json2 "labels[0].label" "Account Number:"
CkJsonObject_UpdateString $json2 "labels[0].value" "0000099999"
set success [CkPdf_TextReplace $pdf2 "check" $json2 $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf2]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkPdf $pdf2
    delete_CkJsonObject $json2
    exit
}

puts "Verdana, not embedded: ok = [CkJsonObject_BoolOf $jsonOut ok], reason: [CkJsonObject_stringOf $jsonOut {problems[0].reason}], font: [CkJsonObject_stringOf $jsonOut {problems[0].font}]"

#  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

delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $pdf2
delete_CkJsonObject $json2