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

Tcl

load ./chilkat.dll

set 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

set pdf [new_CkPdf]

set success [CkPdf_LoadFile $pdf "qa_data/pdf/invoice_labels.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    exit
}

set json [new_CkJsonObject]

set jsonOut [new_CkJsonObject]

#  "Tracking Number:" is not on this invoice.
CkJsonObject_UpdateString $json "labels[0].label" "Invoice Number:"
CkJsonObject_UpdateString $json "labels[0].value" "INV-2026-0500"
CkJsonObject_UpdateString $json "labels[1].label" "Tracking Number:"
CkJsonObject_UpdateString $json "labels[1].value" "1Z999AA10123456784"

set success [CkPdf_TextReplace $pdf "check" $json $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    exit
}

puts "default: ok = [CkJsonObject_BoolOf $jsonOut ok], problem: \"[CkJsonObject_stringOf $jsonOut {problems[0].label}]\" [CkJsonObject_stringOf $jsonOut {problems[0].reason}]"

#  With "ignore", the label that is not there is skipped.
CkJsonObject_UpdateString $json "unmatchedValues" "ignore"
set success [CkPdf_TextReplace $pdf "apply" $json $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    exit
}

puts "ignore: applied = [CkJsonObject_BoolOf $jsonOut applied], replacements: [CkJsonObject_SizeOfArray $jsonOut replacements]"

set success [CkPdf_WriteFile $pdf "qa_output/invoice_renumbered.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    exit
}

#  Sample output:

#  default: ok = false, problem: "Tracking Number:" notFound
#  ignore: applied = true, replacements: 1

delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut