DataFlex Requires Chilkat v11.7.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPdf
Variant vJson
Handle hoJson
Variant vSbText
Handle hoSbText
Variant vJsonOut
Handle hoJsonOut
String sTemp1
Integer iTemp1
Move False To iSuccess
// 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
Get Create (RefClass(cComChilkatPdf)) To hoPdf
If (Not(IsComObjectCreated(hoPdf))) Begin
Send CreateComObject of hoPdf
End
Get ComLoadFile Of hoPdf "qa_data/pdf/long_report.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComNumPages Of hoPdf To iTemp1
Showln "Number of pages: " iTemp1
// 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.
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbText
If (Not(IsComObjectCreated(hoSbText))) Begin
Send CreateComObject of hoSbText
End
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
If (Not(IsComObjectCreated(hoJsonOut))) Begin
Send CreateComObject of hoJsonOut
End
Get pvComObject of hoJson to vJson
Get pvComObject of hoSbText to vSbText
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractTextSb Of hoPdf "text" vJson vSbText vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComLength Of hoSbText To iTemp1
Showln "Extracted " iTemp1 " characters."
// Save the text to a file.
Get ComWriteFile Of hoSbText "qa_output/long_report.txt" "utf-8" False To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSbText To sTemp1
Showln sTemp1
Procedure_Return
End
// The JSON output does not contain the text, but it still has the quality report.
Get ComStringOf Of hoJsonOut "quality.rating" To sTemp1
Showln "Quality: " sTemp1
Get ComStringOf Of hoJsonOut "quality.pagesNeedingOcr" To sTemp1
Showln "Pages with no text (need OCR): [" sTemp1 "]"
// Sample output:
// Number of pages: 3
// Extracted 4522 characters.
// Quality: good
// Pages with no text (need OCR): []
End_Procedure