Extract the text in RTF string programmatically

  • Thread starter Thread starter GD
  • Start date Start date
G

GD

Is there any way or class for extracting text from RTF string
programmatically without any visual control? For example, get the string
"Text in RTF" from
"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fprq7\fcharset0
Humnst777 BT;}} {\colortbl
;\red255\green255\blue153;\red255\green255\blue255;\red0\green0\blue0;}
\viewkind4\uc1\pard\cf1\lang0\f0\fs72 Text in RTF\cf2\highlight3\par }"

GD
 
If all you need is to convert rtf into regular text then I think you should
be able to use the windows rich text box class just doesn't show it.

-------------------------
System.Windows.Forms.RichTextBox rtb = new
System.Windows.Forms.RichTextBox();

rtb.Rtf =
@"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0
Microsoft Sans Serif;}}\viewkind4\uc1\pard\f0\fs17 Hello World\par}";

Console.WriteLine(rtb.Text);
Console.Read();
 
That's definitely an option, but what if it is not a Windows Forms
application (console, ASP.NET, service)?
 
Good idea for winform or webform apps. However, I am using this function in
a web service app. NRTFTree class works great in my case.

Thanks.

GD
 
Hi Nicholas

Not sure what you mean. The sample I posted was done using a console
application. I had to add reference to the "System.Windows.Form.dll" but I
was able to do it that way.

Since I probably know about 5% of what you know, something tells me that I
am missing something :)

Does the Rich Textbox Requires to have messages pumped into it in order to
work reliable?

Cheers.


Nicholas Paldino said:
That's definitely an option, but what if it is not a Windows Forms
application (console, ASP.NET, service)?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rene said:
If all you need is to convert rtf into regular text then I think you
should be able to use the windows rich text box class just doesn't show
it.

-------------------------
System.Windows.Forms.RichTextBox rtb = new
System.Windows.Forms.RichTextBox();

rtb.Rtf =
@"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0
Microsoft Sans Serif;}}\viewkind4\uc1\pard\f0\fs17 Hello World\par}";

Console.WriteLine(rtb.Text);
Console.Read();
 
Rene,

Well, it's not obvious that you are running in a console application
(the calls to Console don't mean it is necessarily a console app, as you can
make the same calls in a Windows Forms app).

The reason I bring it up is that the use of the RichTextBox control
requires the thread that the code is being called on to be an STA apartment
in COM, which means that you also have to pump windows messages, which
aren't guaranteed in service applications, ASP.NET applications, etc, etc.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rene said:
Hi Nicholas

Not sure what you mean. The sample I posted was done using a console
application. I had to add reference to the "System.Windows.Form.dll" but I
was able to do it that way.

Since I probably know about 5% of what you know, something tells me that I
am missing something :)

Does the Rich Textbox Requires to have messages pumped into it in order to
work reliable?

Cheers.


Nicholas Paldino said:
That's definitely an option, but what if it is not a Windows Forms
application (console, ASP.NET, service)?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rene said:
If all you need is to convert rtf into regular text then I think you
should be able to use the windows rich text box class just doesn't show
it.

-------------------------
System.Windows.Forms.RichTextBox rtb = new
System.Windows.Forms.RichTextBox();

rtb.Rtf =
@"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0
Microsoft Sans Serif;}}\viewkind4\uc1\pard\f0\fs17 Hello World\par}";

Console.WriteLine(rtb.Text);
Console.Read();
-------------------------


Is there any way or class for extracting text from RTF string
programmatically without any visual control? For example, get the
string "Text in RTF" from
"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fprq7\fcharset0
Humnst777 BT;}} {\colortbl
;\red255\green255\blue153;\red255\green255\blue255;\red0\green0\blue0;}
\viewkind4\uc1\pard\cf1\lang0\f0\fs72 Text in RTF\cf2\highlight3\par }"

GD
 
Got it, thanks!


Nicholas Paldino said:
Rene,

Well, it's not obvious that you are running in a console application
(the calls to Console don't mean it is necessarily a console app, as you
can make the same calls in a Windows Forms app).

The reason I bring it up is that the use of the RichTextBox control
requires the thread that the code is being called on to be an STA
apartment in COM, which means that you also have to pump windows messages,
which aren't guaranteed in service applications, ASP.NET applications,
etc, etc.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rene said:
Hi Nicholas

Not sure what you mean. The sample I posted was done using a console
application. I had to add reference to the "System.Windows.Form.dll" but
I was able to do it that way.

Since I probably know about 5% of what you know, something tells me that
I am missing something :)

Does the Rich Textbox Requires to have messages pumped into it in order
to work reliable?

Cheers.


Nicholas Paldino said:
That's definitely an option, but what if it is not a Windows Forms
application (console, ASP.NET, service)?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

If all you need is to convert rtf into regular text then I think you
should be able to use the windows rich text box class just doesn't show
it.

-------------------------
System.Windows.Forms.RichTextBox rtb = new
System.Windows.Forms.RichTextBox();

rtb.Rtf =
@"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0
Microsoft Sans Serif;}}\viewkind4\uc1\pard\f0\fs17 Hello World\par}";

Console.WriteLine(rtb.Text);
Console.Read();
-------------------------


Is there any way or class for extracting text from RTF string
programmatically without any visual control? For example, get the
string "Text in RTF" from
"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fprq7\fcharset0
Humnst777 BT;}} {\colortbl
;\red255\green255\blue153;\red255\green255\blue255;\red0\green0\blue0;}
\viewkind4\uc1\pard\cf1\lang0\f0\fs72 Text in
RTF\cf2\highlight3\par }"

GD
 
Back
Top