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

Replace the Second (or Every) Occurrence of a Label in a PDF

See more PDF Text Replacement Examples

Demonstrates the "occurrence" member of a label entry. By default the first occurrence of a label, in reading order across the selected pages, is replaced; "occurrence" can be a number for the nth, or "all" for every occurrence. The invoice template has "Ref:" twice.

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

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

    ;  This template's {{placeholders}} are not being filled here: "keep" leaves them as they are
    ;  (the default, "error", would refuse to apply while a placeholder on the page has no value).
    CkJsonObject::ckUpdateString(json,"unusedPlaceholders","keep")

    ;  "Ref:" appears twice on the page (A-1 near the top, A-2 below the totals).
    ;  The default replaces the first occurrence only.
    CkJsonObject::ckUpdateString(json,"labels[0].label","Ref:")
    CkJsonObject::ckUpdateString(json,"labels[0].value","B-7")
    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 "default: " + Str(CkJsonObject::ckIntOf(jsonOut,"replacements[0].count")) + " occurrence replaced"

    ;  The second occurrence only.
    CkJsonObject::ckUpdateString(json,"labels[0].occurrence","2")
    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 "occurrence 2: " + Str(CkJsonObject::ckIntOf(jsonOut,"replacements[0].count")) + " occurrence replaced"

    ;  Every occurrence.
    CkJsonObject::ckUpdateString(json,"labels[0].occurrence","all")
    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 "occurrence all: " + Str(CkJsonObject::ckIntOf(jsonOut,"replacements[0].count")) + " occurrences replaced"

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

    ;  Sample output:

    ;  default: 1 occurrence replaced
    ;  occurrence 2: 1 occurrence replaced
    ;  occurrence all: 2 occurrences replaced


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


    ProcedureReturn
EndProcedure