将RTF文件转换为HTML的方法(借助Office2003)

 
using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.IO;
using  System.Threading;
using  System.Reflection;
using  System.Runtime.InteropServices;
using  Microsoft.Office.Interop.Word;

namespace  Flyer.VST.QuestionTransform
{
    
public class Rtf2Html
    
{
        
public delegate void Delegate_FoundImage(ref string SrcString, string ImageFile);
        
public event Delegate_FoundImage Event_FoundImage;

        
public Rtf2Html()
        
{
            WordApp 
= new ApplicationClass();
        }


        
private ApplicationClass WordApp;

        
private string m_TempFolder;
        
public string TempFolder
        
{
            
get return m_TempFolder; }
            
set { m_TempFolder = value; }
        }


        
public void BeginTransform()
        
{
            
if (!Directory.Exists(m_TempFolder))
            
{
                Directory.CreateDirectory(m_TempFolder);
            }

        }


        
public string Transform(string rtfstring)
        
{
            Guid guid 
= Guid.NewGuid();
            
string rtffilename = Path.Combine(m_TempFolder, guid.ToString() + ".rtf");
            File.WriteAllText(rtffilename, rtfstring);

            
//------------------------------------------------------------------
            object fileName = rtffilename;
            
object readOnly = true;
            
object isVisible = false;
            
object missing = Missing.Value;
            Document doc 
= WordApp.Documents.Open(ref fileName, ref missing, ref readOnly,
                
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                
ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
            
//------------------------------------------------------------------

            
string htmlfilename = Path.Combine(m_TempFolder, guid.ToString() + ".htm");
            
//----------------------------------------------------------------------
            fileName = htmlfilename;
            
object Format = (int)WdSaveFormat.wdFormatHTML;
            doc.SaveAs(
ref fileName, ref Format, ref missing, ref missing, ref missing, ref missing, ref missing,
                
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

            
//----------------------------------------------------------------------
            doc.Close(ref missing, ref missing, ref missing);
            Marshal.ReleaseComObject(doc);
            doc 
= null;
            GC.Collect();

            
return ProcessHtml(rtffilename, htmlfilename);
        }


        
private string ProcessHtml(string rtffilename,string htmlfilename)
        
{
            
            
//------------------------------------------------------------------------------------------------------------
            string html = File.ReadAllText(htmlfilename, System.Text.Encoding.Default);

            html 
= html.Replace(" "" ");
            html 
= html.Replace("<o:p></o:p>""");
            html 
= html.Replace("<![if !vml]>""");
            html 
= html.Replace("<![endif]>""");
            html 
= html.Replace("</p>""<br>");
            
//------------------------------------------------------------------------------------------------------------
            int div_startindex, div_endindex;
            div_startindex 
= html.IndexOf("<div");
            div_startindex 
= html.IndexOf(">", div_startindex + 1);
            div_endindex 
= html.LastIndexOf("</div>");
            html 
= html.Substring(div_startindex + 1, div_endindex - div_startindex - 1);
            
//------------------------------------------------------------------------------------------------------------
            int span_startindex, span_endindex;
            
while (true)
            
{
                span_startindex 
= html.IndexOf("<span");
                
if (span_startindex == -1)
                    
break;
                
else
                    span_endindex 
= html.IndexOf("</span>", span_startindex + 1);

                
if (span_endindex == -1)
                    
break;

                
int temp_startindex = html.IndexOf(">", span_startindex + 1);
                
if (temp_startindex == -1)
                    
break;

                html 
= html.Remove(span_endindex, 7);
                html 
= html.Remove(span_startindex, temp_startindex - span_startindex + 1);
            }

            
//------------------------------------------------------------------------------------------------------------
            int vml_startindex, vml_endindex;
            
while (true)
            
{
                vml_startindex 
= html.IndexOf("<!--");
                
if (vml_startindex == -1)
                    
break;
                
else
                    vml_endindex 
= html.IndexOf("-->", vml_startindex + 1);

                
if (vml_endindex == -1)
                    
break;

                html 
= html.Remove(vml_startindex, vml_endindex + 3 - vml_startindex);
            }

            
//------------------------------------------------------------------------------------------------------------
            int p_startindex, p_endindex;
            
while (true)
            
{
                p_startindex 
= html.IndexOf("<p");
                
if (p_startindex == -1)
                    
break;
                
else
                    p_endindex 
= html.IndexOf(">", p_startindex + 1);

                
if (p_endindex == -1)
                    
break;

                html 
= html.Remove(p_startindex, p_endindex - p_startindex + 1);
            }

            
//------------------------------------------------------------------------------------------------------------
            int img_startindex = -1, img_endindex;
            
while (true)
            
{
                img_startindex 
= html.IndexOf("<img", img_startindex + 1);
                
if (img_startindex == -1)
                    
break;
                
else
                    img_endindex 
= html.IndexOf(">", img_startindex + 1);

                
if (img_endindex == -1)
                    
break;

                
string imgstring = html.Substring(img_startindex, img_endindex - img_startindex + 1);

                
int src_startindex, src_endindex;
                src_startindex 
= imgstring.IndexOf("src");
                src_startindex 
= imgstring.IndexOf(""", src_startindex + 1);
                src_endindex 
= imgstring.IndexOf(""", src_startindex + 1);

                
string srcstring = imgstring.Substring(src_startindex + 1, src_endindex - src_startindex - 1);

                DirectoryInfo dirinfo 
=  Directory.GetParent(htmlfilename);

                
string image_path = Path.Combine(dirinfo.FullName,srcstring.Replace('/','/'));

                
if (Event_FoundImage != null)
                
{
                    Event_FoundImage(
ref srcstring, image_path);
                }


                html 
= html.Replace(imgstring, string.Format("<img style="border-left-color:#000000;filter:;border-bottom-color:#000000;border-top-color:#000000;border-right-color:#000000" alt="" src="{0}" border="0">", srcstring));
            }

            
//--------------------------------------------------------------------------------------
            
            File.Delete(rtffilename);
            File.Delete(htmlfilename);
            Directory.Delete(htmlfilename.Replace(
".htm"".files"), true);

            html 
= html.Trim();

            
if(html.EndsWith("<br>"))
            
{
                html 
= html.Substring(0, html.Length - 4);
            }


            
return html;
        }


        
public void EndTransform()
        
{
            
object missing = Missing.Value;
            WordApp.Application.Quit(
ref missing, ref missing, ref missing);
            Marshal.ReleaseComObject(WordApp);
            WordApp 
= null;
            GC.Collect();
        }

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值