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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Pdf2
oleobject loo_Json2

li_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.
loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
    destroy loo_Pdf
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Pdf.LoadFile("qa_data/pdf/quote_subset_font.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    return
end if

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")

//  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...
loo_Json.UpdateString("labels[0].label","Quote #:")
loo_Json.UpdateString("labels[0].value","58314")
li_Success = loo_Pdf.TextReplace("check",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "58314: ok = " + string(loo_JsonOut.BoolOf("ok"))

//  ...but "Q" and "-" are not in the subset (nothing in the quotation's regular text uses them).
loo_Json.UpdateString("labels[0].value","Q-58341")
li_Success = loo_Pdf.TextReplace("check",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "Q-58341: ok = " + string(loo_JsonOut.BoolOf("ok"))
if loo_JsonOut.BoolOf("ok") = 0 then
    //  The problem names the font and the characters it cannot draw.
    Write-Debug "    reason: " + loo_JsonOut.StringOf("problems[0].reason")
    Write-Debug "    font: " + loo_JsonOut.StringOf("problems[0].font")
    Write-Debug "    missing: " + loo_JsonOut.StringOf("problems[0].chars")
end if

//  "apply" with a problem returns false and changes nothing.
li_Success = loo_Pdf.TextReplace("apply",loo_Json,loo_JsonOut)
Write-Debug "apply returned " + string(li_Success) + ", applied = " + string(loo_JsonOut.BoolOf("applied"))

//  2. A font that is not embedded and not one of the standard 14.
loo_Pdf2 = create oleobject
li_rc = loo_Pdf2.ConnectToNewObject("Chilkat.Pdf")

li_Success = loo_Pdf2.LoadFile("qa_data/pdf/letter_verdana.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf2.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Pdf2
    return
end if

loo_Json2 = create oleobject
li_rc = loo_Json2.ConnectToNewObject("Chilkat.JsonObject")

loo_Json2.UpdateString("labels[0].label","Account Number:")
loo_Json2.UpdateString("labels[0].value","0000099999")
li_Success = loo_Pdf2.TextReplace("check",loo_Json2,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf2.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Pdf2
    destroy loo_Json2
    return
end if

Write-Debug "Verdana, not embedded: ok = " + string(loo_JsonOut.BoolOf("ok")) + ", reason: " + loo_JsonOut.StringOf("problems[0].reason") + ", font: " + loo_JsonOut.StringOf("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


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Pdf2
destroy loo_Json2