Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
http://msdn.microsoft.com/en-us/library/cc488002
Example
The following example provides a sample RTF file to be converted. The file contains RTF formatting, such as font information, and it also contains four Unicode characters and four extended ASCII characters. The file is opened, passed to the RichTextBox as RTF, retrieved as text, displayed in a MessageBox, and output to a file in UTF-8 format.
// Save the following RTF file to the same folder as your .exe file, and call it "test.rtf". /* {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}{\f1\fnil\fprq1\fc {\colortbl ;\red0\green128\blue0;\red0\green0\blue0;} {\*\generator Msftedit 5.41.21.2508;}\viewkind4\uc1\pard\f0\fs20 This is the \i Greek \i0 word "psych Here are four extended \b ASCII \b0 characters (Windows code page 1252): \'e2\'e4\u1233?\'e5\cf0\par } */ class ConvertFromRTF { static void Main() { string path = @"test.rtf"; //Create the RichTextBox. (Requires a reference to System.Windows.Forms.dll.) System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox(); // Get the contents of the RTF file. Note that when it is // stored in the string, it is encoded as UTF-16. string s = System.IO.File.ReadAllText(path); // Display the RTF text. System.Windows.Forms.MessageBox.Show(s);
1 of 2
5/25/2012 11:48 AM
How to: Convert RTF to Plain Text (C# Programming Guide) // Convert the RTF to plain text. rtBox.Rtf = s; string plainText = rtBox.Text;
http://msdn.microsoft.com/en-us/library/cc488002
// Display plain text output in MessageBox because console // cannot display Greek letters. System.Windows.Forms.MessageBox.Show(plainText); // Output plain text to file, encoded as UTF-8. System.IO.File.WriteAllText(@"output.txt", plainText);
RTF characters are encoded in eight bits. However, the format does let users specify Unicode characters in addition to extended ASCII characters from specified code pages. Because the RichTextBox.Text property is of type string, the characters are encoded as Unicode UTF-16. Any extended ASCII characters and Unicode characters from the source RTF document are correctly encoded in the text output. If you use the File.WriteAllText method to write the text to disk, the text will be encoded as UTF-8 (without a Byte Order Mark).
See Also
Reference System.Windows.Forms.RichTextBox Other Resources Strings (C# Programming Guide)
Yes
No
Community Content
2012 Microsoft. All rights reserved.
2 of 2
5/25/2012 11:48 AM