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

Extract PDF Text to a StringBuilder and Save to a File

See more PDF Text Extraction Examples

Demonstrates ExtractTextSb, which writes the extracted text to a StringBuilder instead of returning it as a JSON string. Use it for large documents. The JSON output still receives the quality report.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_SbText
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 long_report.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/long_report.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    return
end if

Write-Debug "Number of pages: " + string(loo_Pdf.NumPages)

//  ExtractTextSb takes the same operation and options as ExtractText, but the text
//  goes to a StringBuilder.  This avoids building a very large JSON string for
//  documents with many pages.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_SbText = create oleobject
li_rc = loo_SbText.ConnectToNewObject("Chilkat.StringBuilder")

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

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

Write-Debug "Extracted " + string(loo_SbText.Length) + " characters."

//  Save the text to a file.
li_Success = loo_SbText.WriteFile("qa_output/long_report.txt","utf-8",0)
if li_Success = 0 then
    Write-Debug loo_SbText.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_SbText
    destroy loo_JsonOut
    return
end if

//  The JSON output does not contain the text, but it still has the quality report.
Write-Debug "Quality: " + loo_JsonOut.StringOf("quality.rating")
Write-Debug "Pages with no text (need OCR): [" + loo_JsonOut.StringOf("quality.pagesNeedingOcr") + "]"

//  Sample output:

//  Number of pages: 3
//  Extracted 4522 characters.
//  Quality: good
//  Pages with no text (need OCR): []


destroy loo_Pdf
destroy loo_Json
destroy loo_SbText
destroy loo_JsonOut