Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
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 PDF used in this example is available at order_records.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/order_records.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")

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

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 "Order numbers found: " + 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 loo_JsonOut.StringOf("matches[i].text")
    i = i + 1
loop

//  Find the e-mail addresses, but stop after the first three.
loo_Json.UpdateString("query","[a-z0-9._]+@[a-z]+[.][a-z]+")
loo_Json.UpdateInt("maxResults",3)
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 "First three e-mail addresses:"
i = 0
li_Count_i = loo_JsonOut.SizeOfArray("matches")
do while i < li_Count_i
    loo_JsonOut.I = i
    Write-Debug loo_JsonOut.StringOf("matches[i].text")
    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".
loo_Json.Delete("maxResults")
loo_Json.UpdateBool("regex",0)
loo_Json.UpdateString("query","cat")
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 "Occurrences of cat (any): " + string(loo_JsonOut.IntOf("count"))

loo_Json.UpdateBool("wholeWord",1)
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 "Occurrences of cat (whole word): " + string(loo_JsonOut.IntOf("count"))

//  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


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut