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

PDF Placeholders Without a Value: Error, Blank or Keep

See more PDF Text Replacement Examples

Demonstrates the "unusedPlaceholders" option, which decides what happens to a placeholder found on the page that has no value in "placeholders": "error" (the default) makes it a problem with the reason unusedPlaceholder, so nothing is applied; "blank" replaces it with nothing; "keep" leaves it on the page as it is.

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 invoice_template.pdf

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

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

    ;  Values for every placeholder except {{po}} (the purchase order number, which this customer has none of).
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"placeholders.customer","Acme Corp")
    CkJsonObject::ckUpdateString(json,"placeholders.date","2026-10-18")
    CkJsonObject::ckUpdateString(json,"placeholders.qty1","2")
    CkJsonObject::ckUpdateString(json,"placeholders.desc1","Cordless drill, 18 V")
    CkJsonObject::ckUpdateString(json,"placeholders.price1","$460.00")
    CkJsonObject::ckUpdateString(json,"placeholders.amount1","$920.00")
    CkJsonObject::ckUpdateString(json,"placeholders.qty2","6")
    CkJsonObject::ckUpdateString(json,"placeholders.desc2","Galvanized hinge, 4 inch")
    CkJsonObject::ckUpdateString(json,"placeholders.price2","$12.50")
    CkJsonObject::ckUpdateString(json,"placeholders.amount2","$75.00")
    CkJsonObject::ckUpdateString(json,"placeholders.subtotal","$995.00")
    CkJsonObject::ckUpdateString(json,"placeholders.taxRate","7.35%")
    CkJsonObject::ckUpdateString(json,"placeholders.tax","$73.13")
    CkJsonObject::ckUpdateString(json,"placeholders.total","$1,068.13")

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

    ;  The default, "error": the unfilled {{po}} is a problem and nothing would be applied.
    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 "error (default): ok = " + Str(CkJsonObject::ckBoolOf(jsonOut,"ok")) + ", problem: " + CkJsonObject::ckStringOf(jsonOut,"problems[0].field") + " " + CkJsonObject::ckStringOf(jsonOut,"problems[0].reason")

    ;  "keep": {{po}} stays on the page, literally.
    CkJsonObject::ckUpdateString(json,"unusedPlaceholders","keep")
    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 "keep: ok = " + Str(CkJsonObject::ckBoolOf(jsonOut,"ok"))

    ;  "blank": {{po}} is replaced by nothing, leaving "PO Number:" with an empty value.
    CkJsonObject::ckUpdateString(json,"unusedPlaceholders","blank")
    success = CkPdf::ckTextReplace(pdf,"apply",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "blank: applied = " + Str(CkJsonObject::ckBoolOf(jsonOut,"applied"))

    success = CkPdf::ckWriteFile(pdf,"qa_output/invoice_no_po.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    ;  Sample output:

    ;  error (default): ok = false, problem: po unusedPlaceholder
    ;  keep: ok = true
    ;  blank: applied = true


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


    ProcedureReturn
EndProcedure