AutoIt Requires Chilkat v11.7.0+
AutoIt
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 AutoIt Downloads
Local $bSuccess = False
; 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
$oPdf = ObjCreate("Chilkat.Pdf")
$bSuccess = $oPdf.LoadFile("qa_data/pdf/invoice_labels.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
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.
$oJson = ObjCreate("Chilkat.JsonObject")
$oJson.UpdateString("labels[0].label","Invoice Number:")
$oJson.UpdateString("labels[0].value","INV-2026-0500")
$oJson.UpdateString("labels[1].label","Invoice Date:")
$oJson.UpdateString("labels[1].value","April 2, 2026")
$oJson.UpdateString("labels[2].label","customer")
$oJson.UpdateString("labels[2].value","Contoso Ltd")
$oJson.UpdateString("labels[3].label","Total Due:")
$oJson.UpdateString("labels[3].value","$1,999.99")
$oJsonOut = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oPdf.TextReplace("apply",$oJson,$oJsonOut)
If ($bSuccess = False) Then
ConsoleWrite("Not applied: " & $oJsonOut.Emit() & @CRLF)
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
EndIf
Local $i = 0
Local $iCount_i = $oJsonOut.SizeOfArray("replacements")
While $i < $iCount_i
$oJsonOut.I = $i
ConsoleWrite("Replaced the value of """ & $oJsonOut.StringOf("replacements[i].label") & """ on page " & $oJsonOut.StringOf("replacements[i].pages") & @CRLF)
$i = $i + 1
Wend
$bSuccess = $oPdf.WriteFile("qa_output/invoice_reissued.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
; Read the new values back with ExtractText's label option.
$oJsonRead = ObjCreate("Chilkat.JsonObject")
$oJsonText = ObjCreate("Chilkat.JsonObject")
$oJsonRead.UpdateString("label","Total Due")
$oJsonRead.UpdateString("labelValue","right")
$bSuccess = $oPdf.ExtractText("text",$oJsonRead,$oJsonText)
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("Total Due is now: " & $oJsonText.StringOf("text") & @CRLF)
; 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