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();
}
}
}
本文介绍了一个使用Microsoft Word Interop实现的RTF到HTML的转换器。该转换器能够处理复杂的RTF文档,并将其转换为简洁的HTML格式,同时支持图片替换及清理不必要的HTML标签。

1046

被折叠的 条评论
为什么被折叠?



