PureBasic Requires Chilkat v11.7.0+
PureBasic
Get the Text Block Below a PDF Caption
See more PDF Text Extraction Examples
Demonstrates the "label" option with labelValue "below", which captures the block of text directly under a caption such as "Ship To", ending at the next gap or heading. Combined with layout "lines", the lines of an address block are kept separate.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 packing slip used in this example is available at packing_slip.pdf
pdf.i = CkPdf::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/packing_slip.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 packing slip has the captions "Bill To" and "Ship To" side by side,
; each with an address block below it.
CkJsonObject::ckUpdateString(json,"label","Ship To")
; "below" means the value is the block of text directly under the label.
CkJsonObject::ckUpdateString(json,"labelValue","below")
; With the default "flow" layout the lines of the block would be joined into one paragraph.
; "lines" keeps each line of the address on its own line.
CkJsonObject::ckUpdateString(json,"layout","lines")
success = CkPdf::ckExtractText(pdf,"text",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "Ship To:"
Debug CkJsonObject::ckStringOf(jsonOut,"text")
Debug ""
CkJsonObject::ckUpdateString(json,"label","Bill To")
success = CkPdf::ckExtractText(pdf,"text",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "Bill To:"
Debug CkJsonObject::ckStringOf(jsonOut,"text")
Debug ""
; A caption over a short paragraph works the same way. Here the flow layout is better,
; because the two lines are one piece of text.
CkJsonObject::ckUpdateString(json,"label","Special Instructions")
CkJsonObject::ckUpdateString(json,"layout","flow")
success = CkPdf::ckExtractText(pdf,"text",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "Special Instructions:"
Debug CkJsonObject::ckStringOf(jsonOut,"text")
; Sample output:
; Ship To:
; Frank Delgado
; c/o Harbor Pet Boarding
; 9 Ferry Lane
; Portside, WA 98001
;
; Bill To:
; Frank Delgado
; Apartment 3B
; 41 Mill Street
; Portside, WA 98000
;
; Special Instructions:
; Leave the carton with the boarding office if no one answers. Do not leave it outside; the cat tree is not packed for rain.
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndProcedure