PureBasic Requires Chilkat v11.7.0+
PureBasic
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 PureBasic Downloads
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_labels.pdf
pdf.i = CkPdf::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/invoice_labels.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
; "Tracking Number:" is not on this invoice.
CkJsonObject::ckUpdateString(json,"labels[0].label","Invoice Number:")
CkJsonObject::ckUpdateString(json,"labels[0].value","INV-2026-0500")
CkJsonObject::ckUpdateString(json,"labels[1].label","Tracking Number:")
CkJsonObject::ckUpdateString(json,"labels[1].value","1Z999AA10123456784")
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: ok = " + Str(CkJsonObject::ckBoolOf(jsonOut,"ok")) + ", problem: " + Chr(34) + CkJsonObject::ckStringOf(jsonOut,"problems[0].label") + Chr(34) + " " + CkJsonObject::ckStringOf(jsonOut,"problems[0].reason")
; With "ignore", the label that is not there is skipped.
CkJsonObject::ckUpdateString(json,"unmatchedValues","ignore")
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 "ignore: applied = " + Str(CkJsonObject::ckBoolOf(jsonOut,"applied")) + ", replacements: " + Str(CkJsonObject::ckSizeOfArray(jsonOut,"replacements"))
success = CkPdf::ckWriteFile(pdf,"qa_output/invoice_renumbered.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
; Sample output:
; default: ok = false, problem: "Tracking Number:" notFound
; ignore: applied = true, replacements: 1
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndProcedure