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

Search a PDF with a Regular Expression

See more PDF Text Extraction Examples

Demonstrates searching a PDF with a regular expression (regex: true), limiting the number of results (maxResults), and matching whole words only (wholeWord).

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 order_records.pdf

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

set json = Server.CreateObject("Chilkat.JsonObject")
set jsonOut = Server.CreateObject("Chilkat.JsonObject")

'  Find every order number.  With regex set to true, the query is a regular expression.
success = json.UpdateString("query","ORD-[0-9]{4}-[0-9]{4}")
success = json.UpdateBool("regex",1)
'  We only want the matched text, so no context is needed.
success = json.UpdateInt("context",0)

success = pdf.ExtractText("search",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Order numbers found: " & jsonOut.IntOf("count")) & "</pre>"
i = 0
count_i = jsonOut.SizeOfArray("matches")
Do While i < count_i
    jsonOut.I = i
    Response.Write "<pre>" & Server.HTMLEncode( jsonOut.StringOf("matches[i].text")) & "</pre>"
    i = i + 1
Loop

'  Find the e-mail addresses, but stop after the first three.
success = json.UpdateString("query","[a-z0-9._]+@[a-z]+[.][a-z]+")
success = json.UpdateInt("maxResults",3)
success = pdf.ExtractText("search",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "First three e-mail addresses:") & "</pre>"
i = 0
count_i = jsonOut.SizeOfArray("matches")
Do While i < count_i
    jsonOut.I = i
    Response.Write "<pre>" & Server.HTMLEncode( jsonOut.StringOf("matches[i].text")) & "</pre>"
    i = i + 1
Loop

'  wholeWord: true matches only whole words.  The word "cat" appears once in the PDF,
'  but it is also inside "catalog" and "category".
success = json.Delete("maxResults")
success = json.UpdateBool("regex",0)
success = json.UpdateString("query","cat")
success = pdf.ExtractText("search",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Occurrences of cat (any): " & jsonOut.IntOf("count")) & "</pre>"

success = json.UpdateBool("wholeWord",1)
success = pdf.ExtractText("search",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Occurrences of cat (whole word): " & jsonOut.IntOf("count")) & "</pre>"

'  Sample output:

'  Order numbers found: 9
'  ORD-2026-0301
'  ORD-2026-0302
'  ORD-2026-0303
'  ORD-2026-0304
'  ORD-2026-0305
'  ORD-2026-0306
'  ORD-2026-0307
'  ORD-2026-0308
'  ORD-2026-0307
'  First three e-mail addresses:
'  alice.moore@example.com
'  bkumar@example.org
'  carla@example.net
'  Occurrences of cat (any): 3
'  Occurrences of cat (whole word): 1

%>
</body>
</html>