查了一下网上,全都是:下面这段代码
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); // 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); } }
如果有很多rtf String 转换为 txt String ,感觉太没效率!!!
想分析一下rtf格式,自己写一个函数,感觉太复杂,如果不能
public string GetPackageDataAdd()
{
//Data |||| KeyValue |||| FirstChildKeyValue |||| NextSiblingKeyValue |||| DispalyText |||| DisplayForeColor |||| DisplayBackColor |||| DispalyFont + |||| Length
// Length = |||| KeyValue |||| FirstChildKeyValue |||| NextSiblingKeyValue |||| DispalyText |||| DisplayForeColor |||| DisplayBackColor |||| DispalyFont + ||||
if (KeyValue == -1 || DispalyText == null || DispalyText == "" || DispalyFont == null )
throw new Exception(@"KeyValue == -1 || DispalyText == null || DispalyText == "" || DispalyFont == null");
string sAdd = m_Split + KeyValue.ToString() + m_Split + FirstChildKeyValue.ToString() + m_Split + NextSiblingKeyValue.ToString() + m_Split + DispalyText + m_Split;
System.Drawing.ColorConverter cc = new System.Drawing.ColorConverter();
string tmp = cc.ConvertToString(DisplayForeColor);
sAdd = sAdd + tmp + m_Split;
tmp = cc.ConvertToString(DisplayBackColor);
sAdd = sAdd + tmp + m_Split;
System.Drawing.FontConverter fc = new System.Drawing.FontConverter();
tmp = fc.ConvertToString(DispalyFont);
sAdd += tmp;
sAdd += m_Split;
sAdd += sAdd.Length.ToString();
return sAdd;
}
其中Data就是rtf文本,如果只打包txt文本,RichTextBox效果就显示不效果来,
上面这段代码就要重新打包,把 rtf 文本 和 txt文本都打包进去。