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

Replace PDF Text on Selected Pages Only

See more PDF Text Replacement Examples

Demonstrates the "pages" option, which limits the search for placeholders and labels to the given pages: "all" (the default), "first", "last", "odd", "even", or a list such as "1,3,5-9". The two-page order confirmation carries the same "Status:" label on both pages; the status is changed on page 2 only, then on both pages.

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 order_confirmation.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/order_confirmation.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")

//  Both pages have "Status:   Pending".  With pages "2", only page 2 is searched.
loo_Json.UpdateString("pages","2")
loo_Json.UpdateString("labels[0].label","Status:")
loo_Json.UpdateString("labels[0].value","Shipped")
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 "pages 2: found on page(s) " + loo_JsonOut.StringOf("replacements[0].pages")

//  With every page searched, the default occurrence ("first") still replaces one -- the first in reading order.
loo_Json.UpdateString("pages","all")
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 "pages all, first occurrence: found on page(s) " + loo_JsonOut.StringOf("replacements[0].pages")

//  Every occurrence on every page.
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 "pages all, every occurrence: " + string(loo_JsonOut.IntOf("replacements[0].count")) + " replaced on page(s) " + loo_JsonOut.StringOf("replacements[0].pages")

li_Success = loo_Pdf.WriteFile("qa_output/order_shipped.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:

//  pages 2: found on page(s) 2
//  pages all, first occurrence: found on page(s) 1
//  pages all, every occurrence: 2 replaced on page(s) 1,2


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut