PureBasic Requires Chilkat v11.7.0+
PureBasic
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 PureBasic Downloads
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 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
pdf.i = CkPdf::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/order_records.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
ProcedureReturn
EndIf
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
jsonOut.i = CkJsonObject::ckCreate()
If jsonOut.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Find every order number. With regex set to true, the query is a regular expression.
CkJsonObject::ckUpdateString(json,"query","ORD-[0-9]{4}-[0-9]{4}")
CkJsonObject::ckUpdateBool(json,"regex",1)
; We only want the matched text, so no context is needed.
CkJsonObject::ckUpdateInt(json,"context",0)
success = CkPdf::ckExtractText(pdf,"search",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "Order numbers found: " + Str(CkJsonObject::ckIntOf(jsonOut,"count"))
i.i = 0
count_i.i = CkJsonObject::ckSizeOfArray(jsonOut,"matches")
While i < count_i
CkJsonObject::setCkI(jsonOut, i)
Debug CkJsonObject::ckStringOf(jsonOut,"matches[i].text")
i = i + 1
Wend
; Find the e-mail addresses, but stop after the first three.
CkJsonObject::ckUpdateString(json,"query","[a-z0-9._]+@[a-z]+[.][a-z]+")
CkJsonObject::ckUpdateInt(json,"maxResults",3)
success = CkPdf::ckExtractText(pdf,"search",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "First three e-mail addresses:"
i = 0
count_i = CkJsonObject::ckSizeOfArray(jsonOut,"matches")
While i < count_i
CkJsonObject::setCkI(jsonOut, i)
Debug CkJsonObject::ckStringOf(jsonOut,"matches[i].text")
i = i + 1
Wend
; wholeWord: true matches only whole words. The word "cat" appears once in the PDF,
; but it is also inside "catalog" and "category".
CkJsonObject::ckDelete(json,"maxResults")
CkJsonObject::ckUpdateBool(json,"regex",0)
CkJsonObject::ckUpdateString(json,"query","cat")
success = CkPdf::ckExtractText(pdf,"search",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "Occurrences of cat (any): " + Str(CkJsonObject::ckIntOf(jsonOut,"count"))
CkJsonObject::ckUpdateBool(json,"wholeWord",1)
success = CkPdf::ckExtractText(pdf,"search",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "Occurrences of cat (whole word): " + Str(CkJsonObject::ckIntOf(jsonOut,"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
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndProcedure