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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut

li_Success = 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

loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
    destroy loo_Pdf
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Pdf.LoadFile("qa_data/pdf/invoice_template.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    return
end if

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")

//  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).
loo_Json.UpdateString("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.
loo_Json.UpdateString("labels[0].label","Ref:")
loo_Json.UpdateString("labels[0].value","B-7")
li_Success = loo_Pdf.TextReplace("check",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "default: " + string(loo_JsonOut.IntOf("replacements[0].count")) + " occurrence replaced"

//  The second occurrence only.
loo_Json.UpdateString("labels[0].occurrence","2")
li_Success = loo_Pdf.TextReplace("check",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "occurrence 2: " + string(loo_JsonOut.IntOf("replacements[0].count")) + " occurrence replaced"

//  Every occurrence.
loo_Json.UpdateString("labels[0].occurrence","all")
li_Success = loo_Pdf.TextReplace("apply",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "occurrence all: " + string(loo_JsonOut.IntOf("replacements[0].count")) + " occurrences replaced"

li_Success = loo_Pdf.WriteFile("qa_output/invoice_refs.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

//  Sample output:

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


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut