PureBasic Requires Chilkat v11.7.0+
PureBasic
Get a PDF Field Value by its Label (Value to the Right)
See more PDF Text Extraction Examples
Demonstrates the "label" option, which captures the text that belongs to a caption such as "Invoice Number:" without needing coordinates. With labelValue "right", the value is the rest of the label's line. Label matching ignores case, extra spaces and a trailing colon.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 invoice used in this example is available at invoice_labels.pdf
pdf.i = CkPdf::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/invoice_labels.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 invoice has lines such as "Invoice Number: INV-2026-0412".
; The label option restricts the "text" operation to the text belonging to the label.
CkJsonObject::ckUpdateString(json,"label","Invoice Number")
; "right" means the value is the rest of the label's line.
; (The default, "auto", uses the rest of the line if there is text there, otherwise the block below.)
CkJsonObject::ckUpdateString(json,"labelValue","right")
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
; The value is returned as the text.
Debug "Invoice Number: " + CkJsonObject::ckStringOf(jsonOut,"text")
; The labelMatches array tells where the label was found and how it appeared in the PDF.
Debug "Found on page " + Str(CkJsonObject::ckIntOf(jsonOut,"labelMatches[0].page")) + " as " + Chr(34) + CkJsonObject::ckStringOf(jsonOut,"labelMatches[0].label") + Chr(34)
; Matching ignores case and the trailing colon, so these all work:
CkJsonObject::ckUpdateString(json,"label","CUSTOMER:")
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 "Customer: " + CkJsonObject::ckStringOf(jsonOut,"text")
CkJsonObject::ckUpdateString(json,"label","due date")
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 "Due Date: " + CkJsonObject::ckStringOf(jsonOut,"text")
; The totals at the foot of the invoice are right-aligned labels with the amount beside them.
CkJsonObject::ckUpdateString(json,"label","Total Due")
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 "Total Due: " + CkJsonObject::ckStringOf(jsonOut,"text")
; A label that is not found is not an error. The text is empty, labelMatches is empty,
; and the quality report has an issue with the code "labelNotFound".
CkJsonObject::ckUpdateString(json,"label","Tracking Number")
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
If CkJsonObject::ckSizeOfArray(jsonOut,"labelMatches") = 0
Debug "Tracking Number: (not found) " + CkJsonObject::ckStringOf(jsonOut,"quality.pages[0].issues[0].code")
EndIf
; Sample output:
; Invoice Number: INV-2026-0412
; Found on page 1 as "Invoice Number:"
; Customer: Northwind Traders
; Due Date: April 13, 2026
; Total Due: $1,234.53
; Tracking Number: (not found) labelNotFound
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndProcedure