PowerBuilder Requires Chilkat v11.7.0+
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
long i
long li_Count_i
li_Success = 0
// 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
loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
destroy loo_Pdf
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_Pdf.LoadFile("qa_data/pdf/search_context.pdf")
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
return
end if
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")
// The string to find.
loo_Json.UpdateString("query","chilkat")
// The number of characters returned before and after each match. The default is 40.
loo_Json.UpdateInt("context",30)
li_Success = loo_Pdf.ExtractText("search",loo_Json,loo_JsonOut)
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
// "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.
Write-Debug "Matches: " + string(loo_JsonOut.IntOf("count"))
i = 0
li_Count_i = loo_JsonOut.SizeOfArray("matches")
do while i < li_Count_i
loo_JsonOut.I = i
Write-Debug "page " + string(loo_JsonOut.IntOf("matches[i].page")) + ": ..." + loo_JsonOut.StringOf("matches[i].before") + "[" + loo_JsonOut.StringOf("matches[i].text") + "]" + loo_JsonOut.StringOf("matches[i].after") + "..."
i = i + 1
loop
// The search ignores case by default. Set caseSensitive to find only the exact spelling.
loo_Json.UpdateBool("caseSensitive",1)
loo_Json.UpdateString("query","Chilkat")
li_Success = loo_Pdf.ExtractText("search",loo_Json,loo_JsonOut)
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
Write-Debug "Case-sensitive matches for Chilkat: " + string(loo_JsonOut.IntOf("count"))
// 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
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut