This is my solution:
function ExtractPdfText(const FileName: string): string;
var
Doc: TPdfDocument;
i, j, CharCount: Integer;
PageText: string;
begin
Result := '';
Doc := TPdfDocument.Create();
try
Doc.LoadFromFile(FileName);
for i := 0 to Doc.PageCount - 1 do
begin
Doc.ActivePageIndex := i;
CharCount := Doc.Pages[i].GetCharCount;
// Pre-allocate string length for performance
SetLength(PageText, CharCount);
for j := 0 to CharCount - 1 do
PageText[j + 1] := Doc.Pages[i].ReadChar(j);
Result := Result + PageText + sLineBreak;
end;
finally
Doc.Free;
end;
end;