C # convert RTF to plain text

Source: Internet
Author: User

In. NET Framework, you can use Control creation supports RTF and allows users to apply the format to text word processors in WYSIWIG mode.

You can also use Programmatically remove the RTF code from the document to convert the document to plain text. You do not need to embed this control in Windows Forms to perform such operations.

Use the RichTextBox Control in the project
  1. Add a reference to system. Windows. Forms. dll.

  2. (Optional) Add the using command to the system. Windows. Forms namespace ).
Example

The following example provides an example RTF file to be converted. The file contains the RTF format, such as the font information, four Unicode characters and four extended ASCII characters. This file is opened and transmitted , As text retrieval, in And output to a UTF-8 format file.

    // 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\fcharset0 Courier New;}{\f2\fswiss\fprq2\fcharset0 Arial;}}
{\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 "psyche": \cf1\f1\u968?\u965?\u967?\u942?\cf2\f2 . It is encoded in Unicode.\par
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 = @"test2.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);

// Convert the RTF to plain text.
rtBox.Rtf = s;
string plainText = rtBox.Text;

// 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);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.