Sample code for 30+ languages & platforms
AutoIt 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 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 order_records.pdf

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

$oJson = ObjCreate("Chilkat.JsonObject")
$oJsonOut = ObjCreate("Chilkat.JsonObject")

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

$bSuccess = $oPdf.ExtractText("search",$oJson,$oJsonOut)
If ($bSuccess = False) Then
    ConsoleWrite($oPdf.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("Order numbers found: " & $oJsonOut.IntOf("count") & @CRLF)
Local $i = 0
Local $iCount_i = $oJsonOut.SizeOfArray("matches")
While $i < $iCount_i
    $oJsonOut.I = $i
    ConsoleWrite($oJsonOut.StringOf("matches[i].text") & @CRLF)
    $i = $i + 1
Wend

;  Find the e-mail addresses, but stop after the first three.
$oJson.UpdateString("query","[a-z0-9._]+@[a-z]+[.][a-z]+")
$oJson.UpdateInt("maxResults",3)
$bSuccess = $oPdf.ExtractText("search",$oJson,$oJsonOut)
If ($bSuccess = False) Then
    ConsoleWrite($oPdf.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("First three e-mail addresses:" & @CRLF)
$i = 0
$iCount_i = $oJsonOut.SizeOfArray("matches")
While $i < $iCount_i
    $oJsonOut.I = $i
    ConsoleWrite($oJsonOut.StringOf("matches[i].text") & @CRLF)
    $i = $i + 1
Wend

;  wholeWord: true matches only whole words.  The word "cat" appears once in the PDF,
;  but it is also inside "catalog" and "category".
$oJson.Delete("maxResults")
$oJson.UpdateBool("regex",False)
$oJson.UpdateString("query","cat")
$bSuccess = $oPdf.ExtractText("search",$oJson,$oJsonOut)
If ($bSuccess = False) Then
    ConsoleWrite($oPdf.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("Occurrences of cat (any): " & $oJsonOut.IntOf("count") & @CRLF)

$oJson.UpdateBool("wholeWord",True)
$bSuccess = $oPdf.ExtractText("search",$oJson,$oJsonOut)
If ($bSuccess = False) Then
    ConsoleWrite($oPdf.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("Occurrences of cat (whole word): " & $oJsonOut.IntOf("count") & @CRLF)

;  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