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

Visual FoxPro
LOCAL lnSuccess
LOCAL loPdf
LOCAL loJson
LOCAL loSbText
LOCAL loJsonOut

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

loPdf = CreateObject('Chilkat.Pdf')
lnSuccess = loPdf.LoadFile("qa_data/pdf/long_report.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    CANCEL
ENDIF

? "Number of pages: " + STR(loPdf.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.
loJson = CreateObject('Chilkat.JsonObject')
loSbText = CreateObject('Chilkat.StringBuilder')
loJsonOut = CreateObject('Chilkat.JsonObject')

lnSuccess = loPdf.ExtractTextSb("text",loJson,loSbText,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loSbText
    RELEASE loJsonOut
    CANCEL
ENDIF

? "Extracted " + STR(loSbText.Length) + " characters."

*  Save the text to a file.
lnSuccess = loSbText.WriteFile("qa_output/long_report.txt","utf-8",0)
IF (lnSuccess = 0) THEN
    ? loSbText.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loSbText
    RELEASE loJsonOut
    CANCEL
ENDIF

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

*  Sample output:

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

RELEASE loPdf
RELEASE loJson
RELEASE loSbText
RELEASE loJsonOut