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

A PDF Label That Is Not on the Page: notFound or Ignore

See more PDF Text Replacement Examples

Demonstrates the "unmatchedValues" option. A label or placeholder given a value but not found on the selected pages is a problem with the reason notFound, which stops "apply", because a missing label usually means the wrong document or a typo in the label. With unmatchedValues "ignore" such a value is skipped and the rest is applied -- the right choice when one set of values serves several document layouts.

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_labels.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_labels.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")

//  "Tracking Number:" is not on this invoice.
loo_Json.UpdateString("labels[0].label","Invoice Number:")
loo_Json.UpdateString("labels[0].value","INV-2026-0500")
loo_Json.UpdateString("labels[1].label","Tracking Number:")
loo_Json.UpdateString("labels[1].value","1Z999AA10123456784")

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: ok = " + string(loo_JsonOut.BoolOf("ok")) + ", problem: ~"" + loo_JsonOut.StringOf("problems[0].label") + "~" " + loo_JsonOut.StringOf("problems[0].reason")

//  With "ignore", the label that is not there is skipped.
loo_Json.UpdateString("unmatchedValues","ignore")
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 "ignore: applied = " + string(loo_JsonOut.BoolOf("applied")) + ", replacements: " + string(loo_JsonOut.SizeOfArray("replacements"))

li_Success = loo_Pdf.WriteFile("qa_output/invoice_renumbered.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: ok = false, problem: "Tracking Number:" notFound
//  ignore: applied = true, replacements: 1


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut