PureBasic Requires Chilkat v11.7.0+
PureBasic
Check a PDF Text Replacement Before Applying It
See more PDF Text Replacement Examples
Demonstrates the "check" operation, which finds every placeholder and label and applies the font rule but changes nothing, and how to read its report: "ok" says whether "apply" would succeed, "replacements" lists what would be replaced, and "problems" lists what prevents it, each with a reason. Here the first check fails because a placeholder on the page was given no value; the value is added and the check passes, and only then is "apply" called.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_template.pdf
pdf.i = CkPdf::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/invoice_template.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
ProcedureReturn
EndIf
; Values for most of the template's placeholders -- but not {{po}}, {{qty2}}, {{desc2}}, {{price2}} or {{amount2}}.
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json,"placeholders.customer","Acme Corp")
CkJsonObject::ckUpdateString(json,"placeholders.date","2026-10-18")
CkJsonObject::ckUpdateString(json,"placeholders.qty1","2")
CkJsonObject::ckUpdateString(json,"placeholders.desc1","Cordless drill, 18 V")
CkJsonObject::ckUpdateString(json,"placeholders.price1","$460.00")
CkJsonObject::ckUpdateString(json,"placeholders.amount1","$920.00")
CkJsonObject::ckUpdateString(json,"placeholders.subtotal","$920.00")
CkJsonObject::ckUpdateString(json,"placeholders.taxRate","7.35%")
CkJsonObject::ckUpdateString(json,"placeholders.tax","$67.62")
CkJsonObject::ckUpdateString(json,"placeholders.total","$987.62")
; "check" reports what "apply" would do, without changing the PDF.
jsonOut.i = CkJsonObject::ckCreate()
If jsonOut.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
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 "ok: " + Str(CkJsonObject::ckBoolOf(jsonOut,"ok"))
Debug "would replace: " + Str(CkJsonObject::ckSizeOfArray(jsonOut,"replacements")) + " placeholders"
; Each problem names the placeholder (field) or label, and a reason:
; notFound - the placeholder or label is not on the selected pages
; unusedPlaceholder - a placeholder on the page was given no value
; missingGlyph, notInEncoding, nonStandardUnembeddedFont, unsupportedFont - the font rule
; placeholderSpansFonts - the text is drawn with more than one font or size
i.i = 0
count_i.i = CkJsonObject::ckSizeOfArray(jsonOut,"problems")
While i < count_i
CkJsonObject::setCkI(jsonOut, i)
Debug "problem: " + CkJsonObject::ckStringOf(jsonOut,"problems[i].field") + CkJsonObject::ckStringOf(jsonOut,"problems[i].label") + " - " + CkJsonObject::ckStringOf(jsonOut,"problems[i].reason") + " (page " + Str(CkJsonObject::ckIntOf(jsonOut,"problems[i].page")) + ")"
i = i + 1
Wend
; Give the missing placeholders their values and check again.
CkJsonObject::ckUpdateString(json,"placeholders.po","PO-88213")
CkJsonObject::ckUpdateString(json,"placeholders.qty2","0")
CkJsonObject::ckUpdateString(json,"placeholders.desc2","(no second item)")
CkJsonObject::ckUpdateString(json,"placeholders.price2","")
CkJsonObject::ckUpdateString(json,"placeholders.amount2","")
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 "ok after adding the values: " + Str(CkJsonObject::ckBoolOf(jsonOut,"ok"))
; Now apply.
If CkJsonObject::ckBoolOf(jsonOut,"ok") = 1
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 "applied: " + Str(CkJsonObject::ckBoolOf(jsonOut,"applied"))
success = CkPdf::ckWriteFile(pdf,"qa_output/invoice_checked.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
EndIf
; Sample output:
; ok: false
; would replace: 10 placeholders
; problem: po - unusedPlaceholder (page 1)
; problem: qty2 - unusedPlaceholder (page 1)
; problem: desc2 - unusedPlaceholder (page 1)
; problem: price2 - unusedPlaceholder (page 1)
; problem: amount2 - unusedPlaceholder (page 1)
; ok after adding the values: true
; applied: true
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndProcedure