PureBasic Requires Chilkat v11.7.0+
PureBasic
Search for Text in a PDF
See more PDF Text Extraction Examples
Demonstrates the "search" operation, which finds every occurrence of a string in a PDF and returns each match with its page number and the text around it. Matching ignores case unless caseSensitive is true.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 three-page PDF used in this example is available at search_context.pdf
pdf.i = CkPdf::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/search_context.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
; The string to find.
CkJsonObject::ckUpdateString(json,"query","chilkat")
; The number of characters returned before and after each match. The default is 40.
CkJsonObject::ckUpdateInt(json,"context",30)
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
; "count" is the number of matches. Each match has the page, the matched text as it appears
; in the PDF, and the text before and after it.
Debug "Matches: " + Str(CkJsonObject::ckIntOf(jsonOut,"count"))
i.i = 0
count_i.i = CkJsonObject::ckSizeOfArray(jsonOut,"matches")
While i < count_i
CkJsonObject::setCkI(jsonOut, i)
Debug "page " + Str(CkJsonObject::ckIntOf(jsonOut,"matches[i].page")) + ": ..." + CkJsonObject::ckStringOf(jsonOut,"matches[i].before") + "[" + CkJsonObject::ckStringOf(jsonOut,"matches[i].text") + "]" + CkJsonObject::ckStringOf(jsonOut,"matches[i].after") + "..."
i = i + 1
Wend
; The search ignores case by default. Set caseSensitive to find only the exact spelling.
CkJsonObject::ckUpdateBool(json,"caseSensitive",1)
CkJsonObject::ckUpdateString(json,"query","Chilkat")
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 "Case-sensitive matches for Chilkat: " + Str(CkJsonObject::ckIntOf(jsonOut,"count"))
; Sample output:
; Matches: 5
; page 1: ...[Chilkat] River Field Notes The Chilkat...
; page 1: ...Chilkat River Field Notes The [Chilkat] River runs for about fifty mi...
; page 2: ...DAY ONE: UPPER [CHILKAT] FLATS Clear and cold, minus e...
; page 2: ...he river is sometimes spelled [chilkat] in older records, and the val...
; page 3: ...y because the fish run on the [Chilkat] was thinning. Two tagged bird...
; Case-sensitive matches for Chilkat: 3
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndProcedure