Xbase++ Requires Chilkat v11.7.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oPdf
LOCAL oJson
LOCAL oJsonOut
LOCAL i
LOCAL nCount_i
nSuccess := 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
oPdf := CreateObject("Chilkat.Pdf")
nSuccess := oPdf:LoadFile("qa_data/pdf/order_records.pdf")
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
RETURN
ENDIF
oJson := CreateObject("Chilkat.JsonObject")
oJsonOut := CreateObject("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", 1)
// We only want the matched text, so no context is needed.
oJson:UpdateInt("context", 0)
nSuccess := oPdf:ExtractText("search", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
? "Order numbers found: " + Str(oJsonOut:IntOf("count"))
i := 0
nCount_i := oJsonOut:SizeOfArray("matches")
DO WHILE i < nCount_i
oJsonOut:I := i
? oJsonOut:StringOf("matches[i].text")
i := i + 1
ENDDO
// 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)
nSuccess := oPdf:ExtractText("search", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
? "First three e-mail addresses:"
i := 0
nCount_i := oJsonOut:SizeOfArray("matches")
DO WHILE i < nCount_i
oJsonOut:I := i
? oJsonOut:StringOf("matches[i].text")
i := i + 1
ENDDO
// 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", 0)
oJson:UpdateString("query", "cat")
nSuccess := oPdf:ExtractText("search", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
? "Occurrences of cat (any): " + Str(oJsonOut:IntOf("count"))
oJson:UpdateBool("wholeWord", 1)
nSuccess := oPdf:ExtractText("search", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
? "Occurrences of cat (whole word): " + Str(oJsonOut: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
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()