8

I need to convert rtf to plain text. i used to write a function that strips away rtf headers but it is never perfect.

So one option can be using a TRichEdit created at runtime (something like described here but done of course at runtime). But is there another way? Is there an rtl function for this or a better approach?

UPDATE:

In this question I wrote the final version of my rtf to text procedure.

Community
  • 1
  • 1
UnDiUdin
  • 14,924
  • 39
  • 151
  • 249
  • What's wrong with TRichEdit? And who generates the RTF you want to convert? If hope to convert RTF generated from Microsoft Word, the Microsoft-originated TRichEdit might be the best option. – Cosmin Prund Jul 09 '10 at 17:26
  • 2
    @Cosmin Prund: It might seem a bit overkill to create a GUI control (even if it is never displayed on-screen) just to do a file conversion in the background. But of course, it works. – Andreas Rejbrand Jul 09 '10 at 19:46
  • 1
    Exactly, this was my concern. For sure it works. Anyway it seems that is the way. – UnDiUdin Jul 12 '10 at 07:49

2 Answers2

4

If you really want to find a way of doing this conversion you should take a look at how the class TConversion is implemented in the class TRichEditStrings. These classes are in ComCtrls.pas. By understanding how the conversion is done inside the RichEdit controls you may be able to write your own.

However, looking at the practical aspects of implementing this and not knowing what the performance requirements are for your question I would go for the TRichEdit conversion approach outlined in your link.

Alan Fletcher
  • 310
  • 1
  • 4
  • 8
    TRichEditStrings doesn't implement any conversion, all the work is done by the RichEdit control (TRichEdit is nothing but an wrapper around Microsoft's RichEdit control). Writing your own RTF to TEXT implementation is simple for the simple case and very complicated for the complex cases. Microsoft Word can save any document to RTF, retaining all formating. Think about the most weired Word document you ever saw, using the most contorted table structure; Imagine someone saves that as RTF and asks you to convert it to TEXT! – Cosmin Prund Jul 09 '10 at 20:40
  • Ok, I will go for it. There are 2 problems that should be solved in someother way: I need to search in an DB rtf field (for that the trick I am using is to search for the string in the rtf text, it is not the eprfect trick but tipcally works if searching for single words), I need to return "as text" in a grid the content of many rtf fields (here the workaround is returning rtf formatted text). Of course an alternative could be saving also a text field, so for every rtf I save, I save also the text-only version, can be a good approach in a new application,. – UnDiUdin Jul 12 '10 at 07:54
0

The page

  • http://www.delphipraxis.net/152766-rtf-nach-html.html for some pointers to components, libraries and other sources for RTF converters, some of them include a RTF to Text conversion option.

  • ScroogeXHTML, a library for RTF to HTML / XHTML conversion, might be useful. It supports a subset of the RTF standard, and is easy to customize. It has a PlainText export format option. Note: I am the author of ScroogeXHTML for Delphi / ScroogeXHTML for the Java platform. It is a commercial product.

mjn
  • 36,362
  • 28
  • 176
  • 378