问题:不用RichTextBox控件,如何把rtf文本转换为txt文本?

查了一下网上,全都是:下面这段代码

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文本都打包进去。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值