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

Extract One Section of a PDF by its Heading

See more PDF Text Extraction Examples

Demonstrates "labelValue": "section", which returns everything from a heading up to the next heading of the same or higher level. With the "markdown" operation the section keeps its own sub-headings.

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 PDF used in this example is available at sectioned_doc.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/sectioned_doc.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 document has the sections Introduction, Opening Hours, Terms and Conditions (with the
//  sub-sections Admission, Safety and Liability) and Contact.  Get the Terms and Conditions:
//  everything after that heading up to the next heading of the same level (Contact).  The heading
//  itself is not part of the value; labelMatches[0].label has it.
loo_Json.UpdateString("label","Terms and Conditions")
loo_Json.UpdateString("labelValue","section")

li_Success = loo_Pdf.ExtractText("markdown",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 loo_JsonOut.StringOf("markdown")

//  The same with the "text" operation gives the section as plain text.
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 "---- as text ----"
Write-Debug loo_JsonOut.StringOf("text")

//  Sample output:

//  ### Admission
//  
//  Admission is free. Groups of more than ten should book in advance so that a volunteer can be assigned to them.
//  
//  ### Safety
//  
//  No more than eight people may be on the stair at one time. Children under twelve must be accompanied by an adult, and the lantern room is closed in high winds.
//  
//  ### Liability
//  
//  Visitors climb the tower at their own risk. The society carries public liability insurance but accepts no responsibility for personal property.
//  ---- as text ----
//  Admission
//  
//  Admission is free. Groups of more than ten should book in advance so that a volunteer can be assigned to them.
//  
//  Safety
//  
//  No more than eight people may be on the stair at one time. Children under twelve must be accompanied by an adult, and the lantern room is closed in high winds.
//  
//  Liability
//  
//  Visitors climb the tower at their own risk. The society carries public liability insurance but accepts no responsibility for personal property.


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut