Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.7.0+

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oPdf
LOCAL oJson
LOCAL oJsonOut

nSuccess := 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

oPdf := CreateObject("Chilkat.Pdf")
nSuccess := oPdf:LoadFile("qa_data/pdf/invoice_labels.pdf")
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    RETURN
ENDIF

oJson := CreateObject("Chilkat.JsonObject")
oJsonOut := CreateObject("Chilkat.JsonObject")

//  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.
oJson:UpdateString("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.)
oJson:UpdateString("labelValue", "right")

nSuccess := oPdf:ExtractText("text", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

//  The value is returned as the text.
? "Invoice Number: " + oJsonOut:StringOf("text")

//  The labelMatches array tells where the label was found and how it appeared in the PDF.
? "Found on page " + Str(oJsonOut:IntOf("labelMatches[0].page")) + ' as "' + oJsonOut:StringOf("labelMatches[0].label") + '"'

//  Matching ignores case and the trailing colon, so these all work:
oJson:UpdateString("label", "CUSTOMER:")
nSuccess := oPdf:ExtractText("text", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

? "Customer: " + oJsonOut:StringOf("text")

oJson:UpdateString("label", "due date")
nSuccess := oPdf:ExtractText("text", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

? "Due Date: " + oJsonOut:StringOf("text")

//  The totals at the foot of the invoice are right-aligned labels with the amount beside them.
oJson:UpdateString("label", "Total Due")
nSuccess := oPdf:ExtractText("text", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

? "Total Due: " + oJsonOut:StringOf("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".
oJson:UpdateString("label", "Tracking Number")
nSuccess := oPdf:ExtractText("text", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

IF (oJsonOut:SizeOfArray("labelMatches") == 0)
    ? "Tracking Number: (not found) " + oJsonOut:StringOf("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

oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()