adeel825
adeel825

Reputation: 5767

How do I extract/insert text into RTF string in C#

In a C# console app I have the need to extract the text from an RTF string, add some more text to it, and then convert it back into RTF. I have been able to do this using the System.Windows.Forms.RichTextBox class, but I find it a bit odd to use a Forms control in a non-Forms app. Any better way to do this?

Upvotes: 4

Views: 14661

Answers (4)

AMissico
AMissico

Reputation: 21684

There is nothing wrong with using an user-interface control in a console application or even in a web application. The Windows controls are part of the .NET Framework, might as well use them. These controls do not need to be hosted in "forms" in order to work.

Reinventing the wheel, using DLL/ActiveX/OCX, and using Linux are simply not practical answers to your question. The better way is...do what you know. There is actually a performance and maintainence benefit to using existing framework methods then using the suggested alternatives.

Upvotes: 0

Svend
Svend

Reputation: 8158

I think you should just shake this feeling of "odd". There's nothing odd about it.

Upvotes: 1

seanyboy
seanyboy

Reputation: 5693

Doing anything with RTF is pretty difficult unless you're using the windows forms. As stated above, using forms is the easiest way to go.

You could write something yourself, but the RTF spec is pretty complicated.
http://www.biblioscape.com/rtf15_spec.htm

Or you could use a conversion DLL / ActiveX object of which there is a large number available. http://www.sautinsoft.com/

Or - If you're doing this from Linux, there are also tools available. A cursory glance throws up UnRTF http://www.gnu.org/software/unrtf/unrtf.html

I haven't included stuff to turn text back to RTF because I think the RTF specification treats and formats text correctly.

Upvotes: 1

samjudson
samjudson

Reputation: 56893

It depends on what you mean by 'better'. You are already using the simplest and easiest way of doing it.

Upvotes: 0

Related Questions