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

jacob使用教程---OFFICE操作几乎万能公式---读写XML jacob的GitHub地址jacob官网(个人感觉不重要)microsoft官方VBA文档(很重要,jacob所有的参数都来自于这里)jacob找COM组件jacob环境配置教程jacob将word转为各种格式提取word中审阅内容java操作XML,本人使用dom4j本人参考常用例子及相关资料。 阅读详情
 
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();
        }

    }

}

RTFTOHTML转换工具:从RTFHTML的实现 在数字化时代,文档的处理和展示已成为日常工作的一部分。RTF(Rich Text Format)和HTML(Hypertext Markup Language)是两种广泛使用的技术,分别用于文本的格式化和网页的构建。RTFHTML转换技术是将RTF文件转换HTML格式的过程,这在内容管理和网络发布中尤为重要。了解这一转换技术,不仅可以提高工作效率,还能优化文件的存储和传输。我们将深入探讨RTFHTML的特性和转换过程中所需的关键技术,以及如何利用现代编程语言,如C#,来实现高效稳定的转换工具。 阅读详情

相关推荐

rtf格式内容转为html展示

1.将rtf格式内容转为html展示 2.支持不同字体,不同颜色等样式

RTFHTML,HTML转TXT(Java版)

本文是关于如何将RTF转成HTMLHTML再转TXT的例子,其中涉及到Java编码转换,如何还原转义HTML字符等常见问题。

KNIGHTRCOM(rcom10002的专栏) 7676

RTFHTMLHTMLRTF, WPF

RTFHTMLHTMLRTF, WPF

RTFHTML(div>标签)格式的方法(java)

最近有一个将RTF格式的文件转换HTML格式的需求,网上搜索发现相关资料比较少,能找到的一些资料也年代比较久远。经过一番摸索和测试,终于成功的将RTF转成HTML,并且解决了烦人的中文乱码问题。但是很遗憾,目前RTF文件里面的表格和图片还无法转换(没有找到方案)。 1、首先,我们需要先借助WebCAT里面的RTF2HTML这个类,WebCAT的下载地址为:http://webcat.sourceforge.net/或者xxxxxxx。(你也可以直接参考下面的代码,不用下载...

Rookie_cc的博客 1532

【亲测免费】 推荐项目:RTFHTML的PHP神器 - rtf-html-php

推荐项目:RTFHTML的PHP神器 - rtf-html-php 在当今高度数字化的时代,文档转换成为了数据交换中不可或缺的一环。尤其是对于那些依赖PHP进行后端开发的团队而言,找到一个可靠且纯PHP实现的RTFHTML转换器变得至关重要。今天,我们要推荐的就是这样一款开源项目——rtf-html-php。 项目简介 rtf-html-php是一个简洁而高效的RTFHTML转换库,完全由P...

gitblog_00499的博客 564

Python 将RTF文件转为Word 、PDF、HTML

RTF也称富文本格式,是一种具有良好兼容性的文档格式,可以在不同的操作系统和应用程序之间进行交换和共享。有时出于不同项目的需求,我们可能需要将RTF文件转为其他格式。本文将介如何通过简单的Python代码将RTF文件转换为Word Doc/Docx、PDF、HTML格式。方法支持将加载的RTF文件转为Word、PDF、HTML等文档格式,整个操作和代码都非常简单,大家可以自行尝试。将RTF文件另存为Word Doc或Docx格式。将RTF文件另存为HTML格式。将RTF文件另存为PDF格式。

Eiceblue的专栏 2366

使用 C# 编程对 RTF 文档进行操作 [转载]

笔者正在用C#开发一个名为XWriter的文本编辑器,其中需要提供对RTF文档的支持,以前从没有搞过RTF文档,因此临时突击研究了一下,经过几天的学习研究和实践,对C#操作RTF文档有所了解,因此才可以写出此文给予说明,希望能对其他人学习RTF文档格式有所帮助。   RTF文档格式是微软提出的一种用于描述带格式文本的文档格式,上个世纪就提出来了,一直用到现在,而且很多程序都支持这种格式,微软的O...

weixin_33859844的博客 1056

C#操作RTF文档

.x_5{text-align:justify;text-justify:inter-ideograph;margin-top:0;margin-bottom:2 px} .x_4{color:#0000FF} .x_7{color:#008000;font-size:9 pt} .x_6{color:#808080;font-size:9 pt} .x_1{font-size:12 ...

weixin_34082695的博客 480

office word excel 2010 2003 批量 转换

自从office2007和2010推广应用之后,常碰到的是03与07/10格式的读取与转换问题,对于此问题的解决大多是下载一个兼容包使得03下能读07和10的文档。尽管此方法能解决,但当你不知对方是否会操作电脑或者对方是否是领导都带来问题。自从鄙人碰到了,遂找解决方法。 批量实现本地 03 与10格式的转换 方法1:针对excel文件:下载此软件“Excel文档批量转换 Batch X

ASP.NET OSGI AJAX jQuery ----- 帮助别人得到他想要的,你就能得到你想要 4611

Spire.Office的合理化应用,Spire.Office crack

  Spire.Office for .NET用于创建,编辑,转换和打印Microsoft Word文档的 Word控件。支持Word97-2003,Word2007,Word2010和Word2013。q2315702359可以在Word 97/2003/2007/2010/2013与XML,RTF,TXT,XPS,EPUB,EMF,HTML,ODT和其他格式文件之间双向转换。Spire.Office for .NET还可以将Word文件转换为 PDF和SVG文件格式。操作系统(服务器或客户端)不需要安装

q2315702359的博客 1132

无需安装Microsoft Office和Adobe实现办公文档操作,Spire.Office免费资源清单一览

作为一个独立的 Office 组件,Free Spire.Office 的运行环境无需安装 Microsoft Office 及其他第三方软件。基于安全性、稳定性、可扩展性、效率及价格方面的考虑,Spire.Office 已经成为微软办公套件有力的替代品。友情提示: 免费版有篇幅限制。除了文档篇幅限制外,Free Spire.Office 没有任何警告信息,但我们仅对免费版进行不定期维护。 在加载或操作 Word 文档时,要求 Word 文档不超过 500 个段落,25 个表格。同时将 Word 文档转换

daneas的专栏 2184

Outlook2003数据备份的方法讲解

A.Outlook2003数据备份之账户信息备份 打开“开始→程序→Microsoft Office→Microsoft Office工具→Microsoft Office 2003用户设置保存向导”,打开“Microsoft Office 2003用户设置保存向导”对话框。单击“下一步”。选择“保存本机的设置”,单击“下一步”,在出现的对话框中选择文件保存的位置和文件名...

weixin_34388207的博客 133

latex与word之间的各种转化方法

https://blog.csdn.net/communix/article/details/49965233 https://blog.csdn.net/yaoqi_isee/article/details/71125232 在稿件接收后,经常会遇到出版社要求变更稿件格式,其中最多的是latex变为word格式。如果手工操作,是非常麻烦的,还容易出错。如果钱多得花不了,可以让出版社找人去做这...

weixin_30793643的博客 377

存在服务器端的组件,服务器端 Office 自动化注意事项

服务器端自动化的替代方法Microsoft 极力建议开发人员在需要开发服务器端解决方案时寻找“Office 自动化”的备选方案。 由于 Office 设计上的局限性,更改 Office 配置不足以解决所有的问题。Microsoft 提供了很多强烈推荐的备选方案,这些方案不需要在服务器端安装 Office,而且可以比“自动化”更高效、更迅速地执行大多数常规任务。在将 Office 作为您项目中的...

weixin_30393633的博客 225

基于SSM框架的office应用思路

众所周知Microsoft office系列已经很强大了,强大到已经成为企业、个人、组织关于文档需求首选。 最近我想搭建一个SSM框架,主要功能就是实现对word文档的解析极其提取。 word文档为一个一个的考试题目,题目中包含文字,图片,表格。 思路一:poi/jacob/java2word/RTF 思路二:将文档编译成html。图片、表格单独存在其他的数据库中,文字存在html

Hoop 872

UseOffice .Net 5.0 文档转换好帮手Crack

UseOffice .Net is a robust .NET component to convert between DOC, DOCX, XLS, RTF, PPT, PDF and HTML formats with tables, images, fonts, colors etc. Our component is just a class library, it requires Microsoft Office installed. You can install any version o

控件与插件之大全 1324

Spire.Office for Android 7.6.0 Crack-new

Spire.Office for Android via Java is a combination of each Enterprise-Level Android API offered by E-iceblue. It includes Spire.Doc for Android via Java, Spire.XLS for Android via Java, Spire.Presentation for Android via Java and Spire.PDF for Android

控件与插件之大全 543
上一篇: 要将XX标记为系统必备,必须对其进行强签名
下一篇: 理论上最快的Web数据库分页方法
vicon
博客等级 码龄25年 3粉丝 17原创
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值