DataFlex Requires Chilkat v11.7.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPdf
Variant vJson
Handle hoJson
Variant vJsonOut
Handle hoJsonOut
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 invoice used in this example is available at invoice_labels.pdf
Get Create (RefClass(cComChilkatPdf)) To hoPdf
If (Not(IsComObjectCreated(hoPdf))) Begin
Send CreateComObject of hoPdf
End
Get ComLoadFile Of hoPdf "qa_data/pdf/invoice_labels.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
// 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.
Get ComUpdateString Of hoJson "label" "Invoice Number" To iSuccess
// "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.)
Get ComUpdateString Of hoJson "labelValue" "right" To iSuccess
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractText Of hoPdf "text" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
// The value is returned as the text.
Get ComStringOf Of hoJsonOut "text" To sTemp1
Showln "Invoice Number: " sTemp1
// The labelMatches array tells where the label was found and how it appeared in the PDF.
Get ComIntOf Of hoJsonOut "labelMatches[0].page" To iTemp1
Get ComStringOf Of hoJsonOut "labelMatches[0].label" To sTemp1
Showln "Found on page " iTemp1 ' as "' sTemp1 '"'
// Matching ignores case and the trailing colon, so these all work:
Get ComUpdateString Of hoJson "label" "CUSTOMER:" To iSuccess
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractText Of hoPdf "text" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComStringOf Of hoJsonOut "text" To sTemp1
Showln "Customer: " sTemp1
Get ComUpdateString Of hoJson "label" "due date" To iSuccess
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractText Of hoPdf "text" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComStringOf Of hoJsonOut "text" To sTemp1
Showln "Due Date: " sTemp1
// The totals at the foot of the invoice are right-aligned labels with the amount beside them.
Get ComUpdateString Of hoJson "label" "Total Due" To iSuccess
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractText Of hoPdf "text" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComStringOf Of hoJsonOut "text" To sTemp1
Showln "Total Due: " sTemp1
// 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".
Get ComUpdateString Of hoJson "label" "Tracking Number" To iSuccess
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractText Of hoPdf "text" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComSizeOfArray Of hoJsonOut "labelMatches" To iTemp1
If (iTemp1 = 0) Begin
Get ComStringOf Of hoJsonOut "quality.pages[0].issues[0].code" To sTemp1
Showln "Tracking Number: (not found) " sTemp1
End
// 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
End_Procedure