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

Xbase++
LOCAL nSuccess
LOCAL oPdf
LOCAL oJson
LOCAL oSbText
LOCAL oJsonOut

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

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

? "Number of pages: " + Str(oPdf: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.
oJson := CreateObject("Chilkat.JsonObject")
oSbText := CreateObject("Chilkat.StringBuilder")
oJsonOut := CreateObject("Chilkat.JsonObject")

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

? "Extracted " + Str(oSbText:Length) + " characters."

//  Save the text to a file.
nSuccess := oSbText:WriteFile("qa_output/long_report.txt", "utf-8", 0)
IF (nSuccess == 0)
    ? oSbText:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oSbText:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

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

//  Sample output:

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

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