DataFlex Requires Chilkat v11.7.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPdf
Variant vJson
Handle hoJson
Variant vJsonOut
Handle hoJsonOut
Integer i
Integer iCount_i
String sTemp1
Integer iTemp1
Move False To iSuccess
// 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
Get Create (RefClass(cComChilkatPdf)) To hoPdf
If (Not(IsComObjectCreated(hoPdf))) Begin
Send CreateComObject of hoPdf
End
Get ComLoadFile Of hoPdf "qa_data/pdf/order_records.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
If (Not(IsComObjectCreated(hoJsonOut))) Begin
Send CreateComObject of hoJsonOut
End
// Find every order number. With regex set to true, the query is a regular expression.
Get ComUpdateString Of hoJson "query" "ORD-[0-9]{4}-[0-9]{4}" To iSuccess
Get ComUpdateBool Of hoJson "regex" True To iSuccess
// We only want the matched text, so no context is needed.
Get ComUpdateInt Of hoJson "context" 0 To iSuccess
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractText Of hoPdf "search" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComIntOf Of hoJsonOut "count" To iTemp1
Showln "Order numbers found: " iTemp1
Move 0 To i
Get ComSizeOfArray Of hoJsonOut "matches" To iCount_i
While (i < iCount_i)
Set ComI Of hoJsonOut To i
Get ComStringOf Of hoJsonOut "matches[i].text" To sTemp1
Showln sTemp1
Move (i + 1) To i
Loop
// Find the e-mail addresses, but stop after the first three.
Get ComUpdateString Of hoJson "query" "[a-z0-9._]+@[a-z]+[.][a-z]+" To iSuccess
Get ComUpdateInt Of hoJson "maxResults" 3 To iSuccess
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractText Of hoPdf "search" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "First three e-mail addresses:"
Move 0 To i
Get ComSizeOfArray Of hoJsonOut "matches" To iCount_i
While (i < iCount_i)
Set ComI Of hoJsonOut To i
Get ComStringOf Of hoJsonOut "matches[i].text" To sTemp1
Showln sTemp1
Move (i + 1) To i
Loop
// wholeWord: true matches only whole words. The word "cat" appears once in the PDF,
// but it is also inside "catalog" and "category".
Get ComDelete Of hoJson "maxResults" To iSuccess
Get ComUpdateBool Of hoJson "regex" False To iSuccess
Get ComUpdateString Of hoJson "query" "cat" To iSuccess
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractText Of hoPdf "search" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComIntOf Of hoJsonOut "count" To iTemp1
Showln "Occurrences of cat (any): " iTemp1
Get ComUpdateBool Of hoJson "wholeWord" True To iSuccess
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractText Of hoPdf "search" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComIntOf Of hoJsonOut "count" To iTemp1
Showln "Occurrences of cat (whole word): " iTemp1
// 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
End_Procedure