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

What PDF Text Replacement Does Not Change: Metadata, Links, Form Fields

See more PDF Text Replacement Examples

TextReplace changes the page content only. A placeholder that also appears in the document's metadata (the Title, Author, Subject, Keywords, Creator or Producer), in a link's target URL, or in a form field's value is left as it is and reported in "notReplaced" with "where" saying which. The invoice template's title is "Invoice for {{customer}}" and its "View this invoice online" link points at a URL that holds {{customer}}.

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

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

    CkJsonObject::ckUpdateString(json,"placeholders.customer","Acme Corp")
    ;  (The other placeholders are left as they are for this example.)
    CkJsonObject::ckUpdateString(json,"unusedPlaceholders","keep")

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

    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 "In the page content, customer was replaced " + Str(CkJsonObject::ckIntOf(jsonOut,"replacements[0].count")) + " times."

    ;  Where else the placeholder occurs, untouched:
    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jsonOut,"notReplaced")
    While i < count_i
        CkJsonObject::setCkI(jsonOut, i)
        Debug "not replaced: {{" + CkJsonObject::ckStringOf(jsonOut,"notReplaced[i].field") + "}} in " + CkJsonObject::ckStringOf(jsonOut,"notReplaced[i].where")
        i = i + 1
    Wend

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

    ;  Sample output:

    ;  In the page content, customer was replaced 2 times.
    ;  not replaced: {{customer}} in metadata
    ;  not replaced: {{customer}} in linkTarget


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


    ProcedureReturn
EndProcedure