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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Handle hoPdf2
    Variant vJson2
    Handle hoJson2
    String sTemp1
    String sTemp2
    Boolean bTemp1

    Move False To iSuccess

    //  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.
    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/quote_subset_font.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End

    //  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...
    Get ComUpdateString Of hoJson "labels[0].label" "Quote #:" To iSuccess
    Get ComUpdateString Of hoJson "labels[0].value" "58314" To iSuccess
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComTextReplace Of hoPdf "check" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComBoolOf Of hoJsonOut "ok" To bTemp1
    Showln "58314: ok = " bTemp1

    //  ...but "Q" and "-" are not in the subset (nothing in the quotation's regular text uses them).
    Get ComUpdateString Of hoJson "labels[0].value" "Q-58341" To iSuccess
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComTextReplace Of hoPdf "check" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComBoolOf Of hoJsonOut "ok" To bTemp1
    Showln "Q-58341: ok = " bTemp1
    Get ComBoolOf Of hoJsonOut "ok" To bTemp1
    If (bTemp1 = False) Begin
        //  The problem names the font and the characters it cannot draw.
        Get ComStringOf Of hoJsonOut "problems[0].reason" To sTemp1
        Showln "    reason: " sTemp1
        Get ComStringOf Of hoJsonOut "problems[0].font" To sTemp1
        Showln "    font: " sTemp1
        Get ComStringOf Of hoJsonOut "problems[0].chars" To sTemp1
        Showln "    missing: " sTemp1
    End

    //  "apply" with a problem returns false and changes nothing.
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComTextReplace Of hoPdf "apply" vJson vJsonOut To iSuccess
    Get ComBoolOf Of hoJsonOut "applied" To bTemp1
    Showln "apply returned " iSuccess ", applied = " bTemp1

    //  2. A font that is not embedded and not one of the standard 14.
    Get Create (RefClass(cComChilkatPdf)) To hoPdf2
    If (Not(IsComObjectCreated(hoPdf2))) Begin
        Send CreateComObject of hoPdf2
    End
    Get ComLoadFile Of hoPdf2 "qa_data/pdf/letter_verdana.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson2
    If (Not(IsComObjectCreated(hoJson2))) Begin
        Send CreateComObject of hoJson2
    End
    Get ComUpdateString Of hoJson2 "labels[0].label" "Account Number:" To iSuccess
    Get ComUpdateString Of hoJson2 "labels[0].value" "0000099999" To iSuccess
    Get pvComObject of hoJson2 to vJson2
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComTextReplace Of hoPdf2 "check" vJson2 vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComBoolOf Of hoJsonOut "ok" To bTemp1
    Get ComStringOf Of hoJsonOut "problems[0].reason" To sTemp1
    Get ComStringOf Of hoJsonOut "problems[0].font" To sTemp2
    Showln "Verdana, not embedded: ok = " bTemp1 ", reason: " sTemp1 ", font: " sTemp2

    //  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


End_Procedure