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

Visual FoxPro
LOCAL lnSuccess
LOCAL loPdf
LOCAL loJson
LOCAL loJsonOut
LOCAL i
LOCAL lnCount_i

lnSuccess = 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

loPdf = CreateObject('Chilkat.Pdf')
lnSuccess = loPdf.LoadFile("qa_data/pdf/order_records.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    CANCEL
ENDIF

loJson = CreateObject('Chilkat.JsonObject')
loJsonOut = CreateObject('Chilkat.JsonObject')

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

lnSuccess = loPdf.ExtractText("search",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

? "Order numbers found: " + STR(loJsonOut.IntOf("count"))
i = 0
lnCount_i = loJsonOut.SizeOfArray("matches")
DO WHILE i < lnCount_i
    loJsonOut.I = i
    ? loJsonOut.StringOf("matches[i].text")
    i = i + 1
ENDDO

*  Find the e-mail addresses, but stop after the first three.
loJson.UpdateString("query","[a-z0-9._]+@[a-z]+[.][a-z]+")
loJson.UpdateInt("maxResults",3)
lnSuccess = loPdf.ExtractText("search",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

? "First three e-mail addresses:"
i = 0
lnCount_i = loJsonOut.SizeOfArray("matches")
DO WHILE i < lnCount_i
    loJsonOut.I = i
    ? loJsonOut.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".
loJson.Delete("maxResults")
loJson.UpdateBool("regex",0)
loJson.UpdateString("query","cat")
lnSuccess = loPdf.ExtractText("search",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

? "Occurrences of cat (any): " + STR(loJsonOut.IntOf("count"))

loJson.UpdateBool("wholeWord",1)
lnSuccess = loPdf.ExtractText("search",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

? "Occurrences of cat (whole word): " + STR(loJsonOut.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

RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut