Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut

li_Success = 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

loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
    destroy loo_Pdf
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Pdf.LoadFile("qa_data/pdf/invoice_labels.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    return
end if

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("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.
loo_Json.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.)
loo_Json.UpdateString("labelValue","right")

li_Success = loo_Pdf.ExtractText("text",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

//  The value is returned as the text.
Write-Debug "Invoice Number: " + loo_JsonOut.StringOf("text")

//  The labelMatches array tells where the label was found and how it appeared in the PDF.
Write-Debug "Found on page " + string(loo_JsonOut.IntOf("labelMatches[0].page")) + " as ~"" + loo_JsonOut.StringOf("labelMatches[0].label") + "~""

//  Matching ignores case and the trailing colon, so these all work:
loo_Json.UpdateString("label","CUSTOMER:")
li_Success = loo_Pdf.ExtractText("text",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "Customer: " + loo_JsonOut.StringOf("text")

loo_Json.UpdateString("label","due date")
li_Success = loo_Pdf.ExtractText("text",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "Due Date: " + loo_JsonOut.StringOf("text")

//  The totals at the foot of the invoice are right-aligned labels with the amount beside them.
loo_Json.UpdateString("label","Total Due")
li_Success = loo_Pdf.ExtractText("text",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "Total Due: " + loo_JsonOut.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".
loo_Json.UpdateString("label","Tracking Number")
li_Success = loo_Pdf.ExtractText("text",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

if loo_JsonOut.SizeOfArray("labelMatches") = 0 then
    Write-Debug "Tracking Number: (not found) " + loo_JsonOut.StringOf("quality.pages[0].issues[0].code")
end if

//  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


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut