Tcl Requires Chilkat v11.7.0+
Tcl
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 Tcl Downloads
load ./chilkat.dll
set 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
set pdf [new_CkPdf]
set success [CkPdf_LoadFile $pdf "qa_data/pdf/long_report.pdf"]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
exit
}
puts "Number of pages: [CkPdf_get_NumPages $pdf]"
# 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.
set json [new_CkJsonObject]
set sbText [new_CkStringBuilder]
set jsonOut [new_CkJsonObject]
set success [CkPdf_ExtractTextSb $pdf "text" $json $sbText $jsonOut]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkStringBuilder $sbText
delete_CkJsonObject $jsonOut
exit
}
puts "Extracted [CkStringBuilder_get_Length $sbText] characters."
# Save the text to a file.
set success [CkStringBuilder_WriteFile $sbText "qa_output/long_report.txt" "utf-8" 0]
if {$success == 0} then {
puts [CkStringBuilder_lastErrorText $sbText]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkStringBuilder $sbText
delete_CkJsonObject $jsonOut
exit
}
# The JSON output does not contain the text, but it still has the quality report.
puts "Quality: [CkJsonObject_stringOf $jsonOut quality.rating]"
puts "{Pages with no text (need OCR): [}[CkJsonObject_stringOf $jsonOut quality.pagesNeedingOcr]]"
# Sample output:
# Number of pages: 3
# Extracted 4522 characters.
# Quality: good
# Pages with no text (need OCR): []
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkStringBuilder $sbText
delete_CkJsonObject $jsonOut