• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#将RTF转换为纯文本

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

在 .NET Framework 中,可以使用 控件创建支持 RTF 且支持用户以 WYSIWIG 方式将格式应用于文本的字处理器。

也可以使用 以编程方式将 RTF 格式代码从文档中移除,从而将该文档转换为纯文本。执行这种类型的操作无需在 Windows 窗体中嵌入该控件。

在项目中使用 RichTextBox 控件

  1. 添加对 System.Windows.Forms.dll 的引用。

  2. System.Windows.Forms 命名空间添加 using 指令(可选)。

下面的示例提供一个要转换的示例 RTF 文件。该文件包含 RTF 格式,例如字体信息,还包含四个 Unicode 字符和四个扩展的 ASCII 字符。该文件打开,以 RTF 格式传递到 ,作为文本检索,在 中显示并且输出到一个 UTF-8 格式的文件中。  

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




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
解决C#GetPixel和SetPixel效率问题(转)发布时间:2022-07-10
下一篇:
C#工厂模式发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap