Delphi DLL Requires Chilkat v11.7.0+
Delphi DLL
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 DLL Downloads
var
success: Boolean;
pdf: HCkPdf;
json: HCkJsonObject;
sbText: HCkStringBuilder;
jsonOut: HCkJsonObject;
begin
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
pdf := CkPdf_Create();
success := CkPdf_LoadFile(pdf,'qa_data/pdf/long_report.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
Memo1.Lines.Add('Number of pages: ' + IntToStr(CkPdf_getNumPages(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.
json := CkJsonObject_Create();
sbText := CkStringBuilder_Create();
jsonOut := CkJsonObject_Create();
success := CkPdf_ExtractTextSb(pdf,'text',json,sbText,jsonOut);
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
Memo1.Lines.Add('Extracted ' + IntToStr(CkStringBuilder_getLength(sbText)) + ' characters.');
// Save the text to a file.
success := CkStringBuilder_WriteFile(sbText,'qa_output/long_report.txt','utf-8',False);
if (success = False) then
begin
Memo1.Lines.Add(CkStringBuilder__lastErrorText(sbText));
Exit;
end;
// The JSON output does not contain the text, but it still has the quality report.
Memo1.Lines.Add('Quality: ' + CkJsonObject__stringOf(jsonOut,'quality.rating'));
Memo1.Lines.Add('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): []
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(sbText);
CkJsonObject_Dispose(jsonOut);