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

PureBasic
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 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.
    pdf.i = CkPdf::ckCreate()
    If pdf.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/quote_subset_font.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        ProcedureReturn
    EndIf

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    jsonOut.i = CkJsonObject::ckCreate()
    If jsonOut.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  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::ckUpdateString(json,"labels[0].label","Quote #:")
    CkJsonObject::ckUpdateString(json,"labels[0].value","58314")
    success = CkPdf::ckTextReplace(pdf,"check",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "58314: ok = " + Str(CkJsonObject::ckBoolOf(jsonOut,"ok"))

    ;  ...but "Q" and "-" are not in the subset (nothing in the quotation's regular text uses them).
    CkJsonObject::ckUpdateString(json,"labels[0].value","Q-58341")
    success = CkPdf::ckTextReplace(pdf,"check",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "Q-58341: ok = " + Str(CkJsonObject::ckBoolOf(jsonOut,"ok"))
    If CkJsonObject::ckBoolOf(jsonOut,"ok") = 0
        ;  The problem names the font and the characters it cannot draw.
        Debug "    reason: " + CkJsonObject::ckStringOf(jsonOut,"problems[0].reason")
        Debug "    font: " + CkJsonObject::ckStringOf(jsonOut,"problems[0].font")
        Debug "    missing: " + CkJsonObject::ckStringOf(jsonOut,"problems[0].chars")
    EndIf

    ;  "apply" with a problem returns false and changes nothing.
    success = CkPdf::ckTextReplace(pdf,"apply",json,jsonOut)
    Debug "apply returned " + Str(success) + ", applied = " + Str(CkJsonObject::ckBoolOf(jsonOut,"applied"))

    ;  2. A font that is not embedded and not one of the standard 14.
    pdf2.i = CkPdf::ckCreate()
    If pdf2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckLoadFile(pdf2,"qa_data/pdf/letter_verdana.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf2)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkPdf::ckDispose(pdf2)
        ProcedureReturn
    EndIf

    json2.i = CkJsonObject::ckCreate()
    If json2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json2,"labels[0].label","Account Number:")
    CkJsonObject::ckUpdateString(json2,"labels[0].value","0000099999")
    success = CkPdf::ckTextReplace(pdf2,"check",json2,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf2)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkPdf::ckDispose(pdf2)
        CkJsonObject::ckDispose(json2)
        ProcedureReturn
    EndIf

    Debug "Verdana, not embedded: ok = " + Str(CkJsonObject::ckBoolOf(jsonOut,"ok")) + ", reason: " + CkJsonObject::ckStringOf(jsonOut,"problems[0].reason") + ", font: " + CkJsonObject::ckStringOf(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


    CkPdf::ckDispose(pdf)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jsonOut)
    CkPdf::ckDispose(pdf2)
    CkJsonObject::ckDispose(json2)


    ProcedureReturn
EndProcedure