PureBasic Requires Chilkat v11.7.0+
PureBasic
Replace the Value After a Label in a PDF (Re-issue an Invoice)
See more PDF Text Replacement Examples
Demonstrates replacing the values that follow labels such as "Invoice Number:" on a PDF that has no placeholders -- an ordinary invoice. Each entry of the "labels" array names a label as it appears on the page and the new value; the value replaces the rest of the label's line. Label matching ignores case, extra spaces and a trailing colon. The replaced text is read back with ExtractText to confirm it.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
; The invoice has lines such as "Invoice Number: INV-2026-0412" and "Total Due: $1,234.53".
; Each label's new value replaces whatever follows the label on its line.
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json,"labels[0].label","Invoice Number:")
CkJsonObject::ckUpdateString(json,"labels[0].value","INV-2026-0500")
CkJsonObject::ckUpdateString(json,"labels[1].label","Invoice Date:")
CkJsonObject::ckUpdateString(json,"labels[1].value","April 2, 2026")
CkJsonObject::ckUpdateString(json,"labels[2].label","customer")
CkJsonObject::ckUpdateString(json,"labels[2].value","Contoso Ltd")
CkJsonObject::ckUpdateString(json,"labels[3].label","Total Due:")
CkJsonObject::ckUpdateString(json,"labels[3].value","$1,999.99")
jsonOut.i = CkJsonObject::ckCreate()
If jsonOut.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckTextReplace(pdf,"apply",json,jsonOut)
If success = 0
Debug "Not applied: " + CkJsonObject::ckEmit(jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
EndIf
i.i = 0
count_i.i = CkJsonObject::ckSizeOfArray(jsonOut,"replacements")
While i < count_i
CkJsonObject::setCkI(jsonOut, i)
Debug "Replaced the value of " + Chr(34) + CkJsonObject::ckStringOf(jsonOut,"replacements[i].label") + Chr(34) + " on page " + CkJsonObject::ckStringOf(jsonOut,"replacements[i].pages")
i = i + 1
Wend
success = CkPdf::ckWriteFile(pdf,"qa_output/invoice_reissued.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
; Read the new values back with ExtractText's label option.
jsonRead.i = CkJsonObject::ckCreate()
If jsonRead.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
jsonText.i = CkJsonObject::ckCreate()
If jsonText.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(jsonRead,"label","Total Due")
CkJsonObject::ckUpdateString(jsonRead,"labelValue","right")
success = CkPdf::ckExtractText(pdf,"text",jsonRead,jsonText)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkJsonObject::ckDispose(jsonRead)
CkJsonObject::ckDispose(jsonText)
ProcedureReturn
EndIf
Debug "Total Due is now: " + CkJsonObject::ckStringOf(jsonText,"text")
; Sample output:
; Replaced the value of "Invoice Number:" on page 1
; Replaced the value of "Invoice Date:" on page 1
; Replaced the value of "customer" on page 1
; Replaced the value of "Total Due:" on page 1
; Total Due is now: $1,999.99
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkJsonObject::ckDispose(jsonRead)
CkJsonObject::ckDispose(jsonText)
ProcedureReturn
EndProcedure