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

Xojo Plugin
Dim success As Boolean
success = False

//  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

Dim pdf As New Chilkat.Pdf
success = pdf.LoadFile("qa_data/pdf/long_report.pdf")
If (success = False) Then
    System.DebugLog(pdf.LastErrorText)
    Return
End If

System.DebugLog("Number of pages: " + Str(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.
Dim json As New Chilkat.JsonObject
Dim sbText As New Chilkat.StringBuilder
Dim jsonOut As New Chilkat.JsonObject

success = pdf.ExtractTextSb("text",json,sbText,jsonOut)
If (success = False) Then
    System.DebugLog(pdf.LastErrorText)
    Return
End If

System.DebugLog("Extracted " + Str(sbText.Length) + " characters.")

//  Save the text to a file.
success = sbText.WriteFile("qa_output/long_report.txt","utf-8",False)
If (success = False) Then
    System.DebugLog(sbText.LastErrorText)
    Return
End If

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

//  Sample output:

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