In a 32-bit VCL Application in Windows 10 in Delphi 11 Alexandria, I need to search for text in an RTF file. So I use this function (found here) to extract the plain text from the RTF file:
function RtfToText(const RTF_FilePath: string; ReplaceLineFeedWithSpace: Boolean): string;
var
RTFConverter: TRichEdit;
MyStringStream: TStringStream;
begin
RTFConverter := TRichEdit.CreateParented(HWND_MESSAGE);
try
MyStringStream := TStringStream.Create(RTF_FilePath);
try
RTFConverter.Lines.LoadFromStream(MyStringStream);
RTFConverter.PlainText := True;
RTFConverter.Lines.StrictDelimiter := True;
if ReplaceLineFeedWithSpace then
RTFConverter.Lines.Delimiter := ' '
else
RTFConverter.Lines.Delimiter := #13;
Result := RTFConverter.Lines.DelimitedText;
finally
MyStringStream.Free;
end;
finally
RTFConverter.Free;
end;
end;
However, instead of the RTF file's plain text content, the function gives back the file path of the RTF file!
What is wrong with this function, and how can I efficiently extract the plain text from an RTF file without having to use a parented TRichEdit
control?
TRichEdit
has aFindText()
method. – Nonlinearity