RTFTOHTML类

rtf 简单转换html

由于自己写正则写不好!!还有很多可以修改的地方,只是现在比较忙。

没有用到它,现在发出来!让大家帮我看一看。

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;

namespace RTFTOHTML
{
    public class ColorAndFont
    {
        public ColorAndFont()
        {

        }

        public ColorTable ct=new ColorTable ();
        public FontTable ft = new FontTable();
        /// <summary>
        /// 预先处理字体和表格
        /// </summary>
        /// <param name="Source"></param>
        public void GetColorAndFontStr(string Source)
        {
            string ColorStr;
            string FontStr;
            string RegStr = @"{//colortbl ([/W/w]*;)}";//颜色
            Regex r ;
            Match m;
            r=new Regex(RegStr,RegexOptions.IgnoreCase);
            ColorStr = r.Match(Source).Groups[0].Value.ToString();
            ColorStr = ColorStr.Replace("{//colortbl ", "").Replace("}", "");//得到以;分界;/red255/green128/blue128;/red0/green64/blue128;
            string[] str = ColorStr.Split(';');
            for (int i = 0; i < str.Length-1; i++)
            {
                if (str[i] == "")
                {
                    ct.add("#000000");
                }
                else
                {
                    RegStr = @"//red(/d{0,3})//green(/d{0,3})//blue(/d{0,3})";
                    r=new Regex (RegStr,RegexOptions.IgnoreCase);
                    for(m=r.Match(str[i].ToString());m.Success;m=m.NextMatch())//得到颜色的三个值
                    {
                     int red=Convert.ToInt32(m.Groups[1].Value);
                     int green=Convert.ToInt32(m.Groups[2].Value);
                     int blue=Convert.ToInt32(m.Groups[3].Value);
                        Color rd = Color.FromArgb(red,green,blue);
                        ct.add(ColorTranslator.ToHtml(rd));
                    }
                }

 

using System;
using System.Text;

namespace RTFTOHTML
{
   public class RTFUnEncode
    {
       /// <summary>
       /// rtf 文件中的ansi码转换成简体中文
       /// </summary>
       /// <param name="str">ansi字符</param>
        /// <returns>简体中文</returns>
       public static string RTFUnEncodeString(string str)
       {
           string strb = str.Replace("//'", "%");
           byte[] UnUrlEncodestr = System.Web.HttpUtility.UrlDecodeToBytes(strb);
           return Encoding.GetEncoding(936).GetString(UnUrlEncodestr);
       }
    }
}

 

using System;
using System.IO;
using System.Text;

namespace RTFTOHTML
{
   public class RTFFileStream
    {
       /// <summary>
       /// 返回指定文件的数据流
       /// </summary>
       /// <param name="path">绝对路径</param>
       /// <returns>字符串</returns>
       public static string FileStr(string filepath)
       {
           if (File.Exists(filepath))
           {
               StreamReader sr = new StreamReader(filepath);
               string data = sr.ReadToEnd();
               sr.Close();
               sr.Dispose();
               return data;
           }
           return "";
       }
       /// <summary>
       /// 将HTML保存为html文件
       /// </summary>
       /// <param name="str">生成的HTML编码字符串</param>
       /// <param name="filepath">要保存的文件路径包括文件</param>
       public static void Savestr(string str, string filepath)
       {
               byte[] data = Encoding.Default.GetBytes(str);
               FileStream fs = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.None);
               fs.Write(data, 0, data.Length);
               fs.Close();
               fs.Dispose();
       }
    }
}

 

using System;
using System.Text.RegularExpressions;

namespace RTFTOHTML
{
    public  class RTH
    {
        public RTH()
        {

        }
        private ColorAndFont cf = new ColorAndFont();
        private string mainCSS = string.Empty;
        private string col =string.Empty;
        private string fon = string.Empty;
        private string size = string.Empty;
        private string isb = string.Empty;
        private string isunline = string.Empty;
        private string isi = string.Empty;
        private string aglin = string.Empty;
        private string LiN = string.Empty;
        public void MainRtfToHtml(string source,string path)
        {
            Regex r;
            Match m;
            cf.GetColorAndFontStr(source);
            if (cf.ct.Count != 0 && cf.ft.Count != 0)
            {
                string Regstr = @"/{(.+);/}";
                source = Regex.Replace(source, Regstr, "", RegexOptions.IgnoreCase);
                string RegexText = @"(?<!//par).+";
                string TEXT = string.Empty;
                r = new Regex(RegexText, RegexOptions.IgnoreCase | RegexOptions.Multiline);
                for (m = r.Match(source); m.Success; m = m.NextMatch())
                {
                    string temptext = m.Groups[0].Value.Replace("//r", "").Trim();
                    if (temptext != ""&&temptext!="}")
                    {
                        TEXT += RtfContent(temptext);
                    }
                }
                string temp = "<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.0 Transitional//EN/"><html  xmlns=/"http://www.w3.org/1999/xhtml/"><head>  <title></title> <style type=/"text/css/"><!--{$css}--></style></head> <body>{$cont}</body></html>";
                temp = temp.Replace("{$css}", mainCSS);
                temp = temp.Replace("{$cont}", TEXT);
                RTFFileStream.Savestr(temp, path);
            }
        }
        private string RtfContent(string str)
        {
            string cont=string.Empty;
            Regex r=new Regex(@"[^//']//[A-Za-z0-9]{0,}?(?:/s)(.+)|(//'[A-Za-z0-9]*.+)",RegexOptions.IgnoreCase | RegexOptions.Multiline);
            if (!r.IsMatch(str))
            {
                r = new Regex(@"//[A-Za-z0-9]{0,}?(?:/s)(.+)|(//'[A-Za-z0-9]*.+)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            }
            Match m=r.Match(str);
            for(;m.Success;m=m.NextMatch())
            {
                string tempcont=m.Groups[1].Value+m.Groups[2].Value;
                string controlStr = str.Replace(tempcont, "");
                if (tempcont != "")
                {
                    cont = RTFUnEncode.RTFUnEncodeString(tempcont);
                    cont=FormatRTF(cont,controlStr);
                }
               
            }
            return cont;
        }
        private string FormatRTF(string cont, string contorlstr)
        {
            if (cont.IndexOf("}") != -1)
            {
                if (cont.Trim() == "}")
                {
                    cont = "";
                    return cont;
                }
            }
            string view = Regex.Match(contorlstr, @"^//viewkind([0-5])").Groups[1].Value;
            string col_new = Regex.Match(contorlstr, @"//cf([0-" + cf.ct.Count.ToString() + "])").Groups[1].Value.Trim();
            string fon_new = Regex.Match(contorlstr, @"//f([0-" + cf.ft.Count.ToString() + "])").Groups[1].Value.Trim();          
            string size_new = Regex.Match(contorlstr, @"//fs([10-140])").Groups[1].Value.Trim();
            if(col_new!="")
                col="color:" + cf.ct.GetColor(int.Parse(col_new)) + ";";
            if(fon_new!="")
                fon ="font-family:" + cf.ft.GetFont(int.Parse(fon_new)) + ";";
            if(size_new!="")
                size="font-size:" + size_new + "pt;";
            if (Regex.Match(contorlstr, @"(//b)(?:///w|/s)").Groups[1].Value == "//b")
                isb = "font-weight: bold;";
            else
            {
                if (Regex.Match(contorlstr, @"(//b0)(?:///w|/s)").Groups[1].Value == "//b0")
                    isb = "";
            }
            if (Regex.Match(contorlstr, @"(//i)(?:///w|/s)").Groups[1].Value == "//i")
                isi = " font-style: italic;";
            else
            {
                if (Regex.Match(contorlstr, @"(//i0)(?:///w|/s)").Groups[1].Value == "//i0")
                    isi = "";
            }
            if (Regex.Match(contorlstr, @"(//ul)(?:///w|/s)").Groups[1].Value=="//ul")
                isunline = "text-decoration: underline;";
            else
            {
                if (Regex.Match(contorlstr, @"(//ulnone)(?:///w|/s)").Groups[1].Value == "//ulnone")
                    isunline = "";
            }
            string aglin_new = Regex.Match(contorlstr, @"(//q[c,r])(?:///w|/s|//'|)").Groups[1].Value;
            switch (aglin_new)
            {
                case "//qc": aglin = "text-align:center;"; break;
                case "//qr": aglin = "text-align:right;"; break;
                default: aglin = "text-align:left;"; break;
            }
            string LiN_new = Regex.Match(contorlstr, @"(?://li(/d{0,}))(?:///w|/s|//'|)").Groups[1].Value;
            if (LiN_new != "")
            {
                LiN = "margin-left:" + Convert.ToInt32(LiN_new) / 20 + ".0pt;";
            }
            else
                LiN = "";
            string cfdata = col + fon + size+isb+isi+isunline+aglin+LiN;
            if (view != "")
            {
                mainCSS = "body {margin-left: 0px;margin-top: 0px;margin-right: 0px; margin-bottom: 0px; width:980px; text-align:center;}";
                switch (view)
                {
                    case "4": mainCSS += ".viewkind {width:70%;"+cfdata+"}"; break;
                    default: mainCSS+= ".viewkind {width:100%;"+cfdata+"}"; break;
                }
            }
            cont="<p class=viewkind align=left style='"+cfdata+"'>"+cont+"</p>";
            return cont;
        }
    }
}

下载

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值