Sample code for 30+ languages & platforms
AutoIt Requires Chilkat v11.7.0+

What PDF Text Replacement Does Not Change: Metadata, Links, Form Fields

See more PDF Text Replacement Examples

TextReplace changes the page content only. A placeholder that also appears in the document's metadata (the Title, Author, Subject, Keywords, Creator or Producer), in a link's target URL, or in a form field's value is left as it is and reported in "notReplaced" with "where" saying which. The invoice template's title is "Invoice for {{customer}}" and its "View this invoice online" link points at a URL that holds {{customer}}.

Chilkat AutoIt Downloads

AutoIt
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_template.pdf

$oPdf = ObjCreate("Chilkat.Pdf")
$bSuccess = $oPdf.LoadFile("qa_data/pdf/invoice_template.pdf")
If ($bSuccess = False) Then
    ConsoleWrite($oPdf.LastErrorText & @CRLF)
    Exit
EndIf

$oJson = ObjCreate("Chilkat.JsonObject")
$oJson.UpdateString("placeholders.customer","Acme Corp")
;  (The other placeholders are left as they are for this example.)
$oJson.UpdateString("unusedPlaceholders","keep")

$oJsonOut = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oPdf.TextReplace("apply",$oJson,$oJsonOut)
If ($bSuccess = False) Then
    ConsoleWrite($oPdf.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("In the page content, customer was replaced " & $oJsonOut.IntOf("replacements[0].count") & " times." & @CRLF)

;  Where else the placeholder occurs, untouched:
Local $i = 0
Local $iCount_i = $oJsonOut.SizeOfArray("notReplaced")
While $i < $iCount_i
    $oJsonOut.I = $i
    ConsoleWrite("not replaced: {{" & $oJsonOut.StringOf("notReplaced[i].field") & "}} in " & $oJsonOut.StringOf("notReplaced[i].where") & @CRLF)
    $i = $i + 1
Wend

$bSuccess = $oPdf.WriteFile("qa_output/invoice_customer.pdf")
If ($bSuccess = False) Then
    ConsoleWrite($oPdf.LastErrorText & @CRLF)
    Exit
EndIf

;  Sample output:

;  In the page content, customer was replaced 2 times.
;  not replaced: {{customer}} in metadata
;  not replaced: {{customer}} in linkTarget