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

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 Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 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

set pdf = Server.CreateObject("Chilkat.Pdf")
success = pdf.LoadFile("qa_data/pdf/invoice_template.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

'  Values for most of the template's placeholders -- but not {{po}}, {{qty2}}, {{desc2}}, {{price2}} or {{amount2}}.
set json = Server.CreateObject("Chilkat.JsonObject")
success = json.UpdateString("placeholders.customer","Acme Corp")
success = json.UpdateString("placeholders.date","2026-10-18")
success = json.UpdateString("placeholders.qty1","2")
success = json.UpdateString("placeholders.desc1","Cordless drill, 18 V")
success = json.UpdateString("placeholders.price1","$460.00")
success = json.UpdateString("placeholders.amount1","$920.00")
success = json.UpdateString("placeholders.subtotal","$920.00")
success = json.UpdateString("placeholders.taxRate","7.35%")
success = json.UpdateString("placeholders.tax","$67.62")
success = json.UpdateString("placeholders.total","$987.62")

'  "check" reports what "apply" would do, without changing the PDF.
set jsonOut = Server.CreateObject("Chilkat.JsonObject")
success = pdf.TextReplace("check",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "ok: " & jsonOut.BoolOf("ok")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "would replace: " & jsonOut.SizeOfArray("replacements") & " placeholders") & "</pre>"

'  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 = 0
count_i = jsonOut.SizeOfArray("problems")
Do While i < count_i
    jsonOut.I = i
    Response.Write "<pre>" & Server.HTMLEncode( "problem: " & jsonOut.StringOf("problems[i].field") & jsonOut.StringOf("problems[i].label") & " - " & jsonOut.StringOf("problems[i].reason") _
         & " (page " & jsonOut.IntOf("problems[i].page") & ")") & "</pre>"
    i = i + 1
Loop

'  Give the missing placeholders their values and check again.
success = json.UpdateString("placeholders.po","PO-88213")
success = json.UpdateString("placeholders.qty2","0")
success = json.UpdateString("placeholders.desc2","(no second item)")
success = json.UpdateString("placeholders.price2","")
success = json.UpdateString("placeholders.amount2","")
success = pdf.TextReplace("check",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "ok after adding the values: " & jsonOut.BoolOf("ok")) & "</pre>"

'  Now apply.
If (jsonOut.BoolOf("ok") = 1) Then
    success = pdf.TextReplace("apply",json,jsonOut)
    If (success = 0) Then
        Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
        Response.End
    End If

    Response.Write "<pre>" & Server.HTMLEncode( "applied: " & jsonOut.BoolOf("applied")) & "</pre>"
    success = pdf.WriteFile("qa_output/invoice_checked.pdf")
    If (success = 0) Then
        Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
        Response.End
    End If

End If

'  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

%>
</body>
</html>