2

I use Delphi XE5 (only Windows platform). I need to show RTF documents and be able to extract plain text (for search operations). I created small test app, it has few components:

RichEdit1: TRichEdit;
cxRichEdit1: TcxRichEdit;
WPRichText1: TWPRichText;
Button2: TButton;
Memo1: TMemo;
Memo2: TMemo;
Memo3: TMemo;

and such code (load same RTF file to TRichEdit / TcxRichEdit / TWPRichText and extract plain text to corresponding memos):

  RichEdit1.Lines.LoadFromFile('c:\d\1.rtf');
  cxRichEdit1.Lines.LoadFromFile('c:\d\1.rtf');
  WPRichText1.LoadFromFile('c:\d\1.rtf', True, 'RTF');
  memo1.Text := RichEdit1.Text;
  memo2.Text := cxRichEdit1.Text;
  memo3.Text := WPRichText1.SaveToString('UNICODE');

And i have such results.

TRichEdit:
???????? <http://news.tut.by/politics/> ...

Failed to extract unicode chars (system locale is Norwegian), contains URLs.

TcxRichEdit:
HYPERLINK "http://news.tut.by/politics/"ПОЛИТИКА ...

Unicode chars are ok, but shows URLs as part of the plain text.

TWPRichText:
ПОЛИТИКА

The only component who does the job correctly. But i would like to avoid of use TWPRichText, because it is heavy and slow. Does anyone know, is it possible to solve the problem with standard TRichEdit? Sample of RTF can be downloaded here.

Andrei Galatyn
  • 3,322
  • 2
  • 24
  • 38
  • +1 This is quite odd. The RTF looks weird mind you. What generated it? Try round tripping it through Word and see the difference. – David Heffernan Oct 02 '14 at 12:09
  • @David: It was generated by WPTools (copy/paste from some site), but it seems to be correct. MSWord/WordPad can open it. TcxRichEdit shows it correctly, but inserts URLs into plain text. I need to be able show/extract plaint text from any valid RTF file, not only from "good" files generated with specific apps. – Andrei Galatyn Oct 02 '14 at 12:43
  • Well, the fact that Word/Wordpad can open it doesn't necessarily validate it. But it probably is fine. I wonder whether the issue is that the RTF code in Delphi uses an old version of riched32.dll. Do you need to display the RTF? Perhaps one way out is to use the windowless RTF library and code that by hand. Avoid the VCL altogether. – David Heffernan Oct 02 '14 at 12:46
  • @David: Probably you right, TcxRichEdit has property RichEditClass, if i set it to recRichEdit20 (default value is recRichEdit41), then it behaves same way as TRichEdit. But it means, even if i will be able to use latest lib, then it should solve the problem with unicode chars, but not with URLS. – Andrei Galatyn Oct 02 '14 at 12:52
  • I would also give TRichView (trichview.com) a try, a commercial Delphi component – mjn Oct 02 '14 at 14:58
  • Maybe helpful (going via HTML): http://stackoverflow.com/questions/21612243/how-to-convert-rich-text-rtf-to-plain-or-html-in-delphi-xe4?rq=1 – mjn Oct 02 '14 at 16:52

0 Answers0