Delphi ActiveX Requires Chilkat v11.7.0+
Delphi ActiveX
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 Delphi ActiveX Downloads
var
success: Integer;
pdf: TChilkatPdf;
json: TChilkatJsonObject;
sbText: TChilkatStringBuilder;
jsonOut: TChilkatJsonObject;
begin
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
pdf := TChilkatPdf.Create(Self);
success := pdf.LoadFile('qa_data/pdf/long_report.pdf');
if (success = 0) then
begin
Memo1.Lines.Add(pdf.LastErrorText);
Exit;
end;
Memo1.Lines.Add('Number of pages: ' + IntToStr(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.
json := TChilkatJsonObject.Create(Self);
sbText := TChilkatStringBuilder.Create(Self);
jsonOut := TChilkatJsonObject.Create(Self);
success := pdf.ExtractTextSb('text',json.ControlInterface,sbText.ControlInterface,jsonOut.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(pdf.LastErrorText);
Exit;
end;
Memo1.Lines.Add('Extracted ' + IntToStr(sbText.Length) + ' characters.');
// Save the text to a file.
success := sbText.WriteFile('qa_output/long_report.txt','utf-8',0);
if (success = 0) then
begin
Memo1.Lines.Add(sbText.LastErrorText);
Exit;
end;
// The JSON output does not contain the text, but it still has the quality report.
Memo1.Lines.Add('Quality: ' + jsonOut.StringOf('quality.rating'));
Memo1.Lines.Add('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): []