控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

C#操作.rtf文档

作者:佚名 出处:互联网 2010年12月17日 阅读:

C#操作.rtf文档
 
前段时间做过一些用C#操作.rtf文档的工作,当时就是直接根据RTF的规范用修改文本文件的方式修改.rtf文件。比如要插入一段文字,并且用红色表示,就自己弄一个colortable的定义,然后直接把下面的内容插入到.rtf文件中, {\b\cf1Bold Red!} \b表示黑体,\cf1表示用colortable定义中的第一种颜色。(假设我们的这个rtf文件中定义的第一种颜色是红色)。但是这种办法很不直观,而且有限制,需要编码时知道colortable是怎么定义的。

今天重新用System.Windows.Forms.RichTextBox来实现。下面的代码实现了在原来的rtf文件的最开始插入一行红色黑体的“Alert”,然后在rtf的commens:后面加上“new comment”,最后在末尾加一句“Bye!”。

             private static void RtfTest(string inpath, string outpath)

             {

                 // load rtf file

                 RichTextBox rtf = new RichTextBox();

                 using (StreamReader sr = new StreamReader(inpath))

                 {

                     rtf.Rtf = sr.ReadToEnd();

                 }

    

                // add alert the begining of the rtf file

                rtf.SelectionStart = 0;

                rtf.SelectionLength = 1;

                string alert = "Alert!" + Environment.NewLine;

                rtf.SelectedText = alert + rtf.SelectedText;

                rtf.SelectionStart = 0;

                // length of new line in string is 2, but in rtf is 1, so we need to minus the line count

                rtf.SelectionLength = alert.Length - 1;

                rtf.SelectionColor = Color.Red;

                rtf.SelectionFont = new Font(rtf.SelectionFont, System.Drawing.FontStyle.Bold);

   

   

                // add comment after "Comments:"

                string commentStart = "Comments:";

                string newComment = "new comment";

                rtf.Find(commentStart);

                rtf.SelectedText = rtf.SelectedText + Environment.NewLine + newComment;

                rtf.Find(commentStart);

                rtf.SelectionStart += commentStart.Length;

                rtf.SelectionLength += newComment.Length;

                rtf.SelectionColor = Color.Black;

                rtf.SelectionFont = new Font(rtf.SelectionFont, System.Drawing.FontStyle.Regular);

   

                // add "Bye!" to the end

                rtf.AppendText(Environment.NewLine + "Bye!");

   

                // save the rtf file

                rtf.SaveFile(outpath);

                rtf.Dispose();

                rtf = null;

            }

 

其中有几个需要注意的地方:
1.不能直接给rtf.Text赋值,给Text赋值会导致整个rtf文档的格式丢失。这个示例中使用了selection来实现。
2.string中的换行的length是2,但是rtf中的换行的length是1,根据这个长度设置选择区域是要注意。
3.往rtf文档的最后以行加东西可以直接调用AppendText,会保留原来的格式。

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统