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

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 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 packing slip used in this example is available at packing_slip.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/packing_slip.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 packing slip has the captions "Bill To" and "Ship To" side by side,
//  each with an address block below it.
loo_Json.UpdateString("label","Ship To")

//  "below" means the value is the block of text directly under the label.
loo_Json.UpdateString("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.
loo_Json.UpdateString("layout","lines")

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 "Ship To:"
Write-Debug loo_JsonOut.StringOf("text")
Write-Debug ""

loo_Json.UpdateString("label","Bill To")
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 "Bill To:"
Write-Debug loo_JsonOut.StringOf("text")
Write-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.
loo_Json.UpdateString("label","Special Instructions")
loo_Json.UpdateString("layout","flow")
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 "Special Instructions:"
Write-Debug loo_JsonOut.StringOf("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.


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut