AutoIt Requires Chilkat v11.7.0+
AutoIt
Search for Text in a PDF
See more PDF Text Extraction Examples
Demonstrates the "search" operation, which finds every occurrence of a string in a PDF and returns each match with its page number and the text around it. Matching ignores case unless caseSensitive is true.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 three-page PDF used in this example is available at search_context.pdf
$oPdf = ObjCreate("Chilkat.Pdf")
$bSuccess = $oPdf.LoadFile("qa_data/pdf/search_context.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
$oJson = ObjCreate("Chilkat.JsonObject")
$oJsonOut = ObjCreate("Chilkat.JsonObject")
; The string to find.
$oJson.UpdateString("query","chilkat")
; The number of characters returned before and after each match. The default is 40.
$oJson.UpdateInt("context",30)
$bSuccess = $oPdf.ExtractText("search",$oJson,$oJsonOut)
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
; "count" is the number of matches. Each match has the page, the matched text as it appears
; in the PDF, and the text before and after it.
ConsoleWrite("Matches: " & $oJsonOut.IntOf("count") & @CRLF)
Local $i = 0
Local $iCount_i = $oJsonOut.SizeOfArray("matches")
While $i < $iCount_i
$oJsonOut.I = $i
ConsoleWrite("page " & $oJsonOut.IntOf("matches[i].page") & ": ..." & $oJsonOut.StringOf("matches[i].before") & "[" & $oJsonOut.StringOf("matches[i].text") & "]" _
& $oJsonOut.StringOf("matches[i].after") & "..." & @CRLF)
$i = $i + 1
Wend
; The search ignores case by default. Set caseSensitive to find only the exact spelling.
$oJson.UpdateBool("caseSensitive",True)
$oJson.UpdateString("query","Chilkat")
$bSuccess = $oPdf.ExtractText("search",$oJson,$oJsonOut)
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("Case-sensitive matches for Chilkat: " & $oJsonOut.IntOf("count") & @CRLF)
; Sample output:
; Matches: 5
; page 1: ...[Chilkat] River Field Notes The Chilkat...
; page 1: ...Chilkat River Field Notes The [Chilkat] River runs for about fifty mi...
; page 2: ...DAY ONE: UPPER [CHILKAT] FLATS Clear and cold, minus e...
; page 2: ...he river is sometimes spelled [chilkat] in older records, and the val...
; page 3: ...y because the fish run on the [Chilkat] was thinning. Two tagged bird...
; Case-sensitive matches for Chilkat: 3