Lua Requires Chilkat v11.7.0+
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- 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
local pdf = chilkat.Pdf.new()
local success = pdf:load_file("qa_data/pdf/long_report.pdf")
if not success then
print(pdf.last_error_text)
return
end
print("Number of pages: " .. pdf.num_pages)
-- 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.
local json = chilkat.JsonObject.new()
local sbText = chilkat.StringBuilder.new()
local jsonOut = chilkat.JsonObject.new()
success = pdf:extract_text_sb("text", json, sbText, jsonOut)
if not success then
print(pdf.last_error_text)
return
end
print("Extracted " .. sbText.length .. " characters.")
-- Save the text to a file.
success = sbText:write_file("qa_output/long_report.txt", "utf-8", false)
if not success then
print(sbText.last_error_text)
return
end
-- The JSON output does not contain the text, but it still has the quality report.
print("Quality: " .. jsonOut:string_of("quality.rating"))
print("Pages with no text (need OCR): [" .. jsonOut:string_of("quality.pagesNeedingOcr") .. "]")
-- Sample output:
-- Number of pages: 3
-- Extracted 4522 characters.
-- Quality: good
-- Pages with no text (need OCR): []