在 .NET 中将 RTF 转换为 HTML

发布于 2024-12-29 04:56:25 字数 106 浏览 0 评论 0原文

我已经成功地使用 WebBrowserRichTextBox 完成了相反的操作。

但是如何将 RTF 转换为 HTML 呢?

I've managed to do the reverse using WebBrowser and RichTextBox.

But how would I convert RTF to HTML?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

多彩岁月 2025-01-05 04:56:25

如果您弹出 NuGet 并搜索“RTF”,目前最流行的结果如下所示:RtfPipe;你可以在那里安装它,或者通过包管理器控制台:

Install-Package RtfPipe

然后在你的 C# 中,你可以非常轻松地将 RTF 转换为 HTML:

var html = RtfPipe.Rtf.ToHtml(rtf);

根据 他们的 GitHub 页面

该库尝试支持 RTF 规范 1.9.1 中记录的核心 RTF 功能。这些功能包括:

  • 字符格式(粗体、斜体、颜色……)
  • 表格(包括嵌套表格)
  • 列表
  • 超链接
  • 图片
  • 标题级别
  • HTML 封装(例如由 Outlook 执行)

话虽如此,在很多情况下,对于重要文档,与其他 RTF 阅读器(例如 MS Word)相比,该库无法生成“正确”的视觉表示。

我将 RTF 输入到其中,效果非常好。 YYMV。

If you pop-up NuGet and search for "RTF", the most popular result right now looks like RtfPipe; you can install it right there, or via the package manager console via:

Install-Package RtfPipe

Then in your C#, you can convert RTF to HTML super easily:

var html = RtfPipe.Rtf.ToHtml(rtf);

According to the readme.md on their GitHub page:

This library attempts to support the core RTF features documented in the RTF Specification 1.9.1. These features include:

  • Character formatting (bold, italics, color, ...)
  • Tables (including nested tables)
  • Lists
  • Hyperlinks
  • Pictures
  • Heading levels
  • HTML encapsulation (e.g. as performed by Outlook)

With that said, there are numerous cases for non-trivial documents where the library will not produce the "correct" visual representation when compared to other RTF readers (such as MS Word).

I piped my RTF into it, and it worked amazingly. YYMV.

恋竹姑娘 2025-01-05 04:56:25

免责声明:我在这家公司工作。

正如我所见,这个问题很老了,但也许有人也在寻找解决方案。我们的 RTF to HTML 组件允许将 RTF 转换为 HTML。您可以下载一个组件或尝试在线演示。如果您有疑问,请先尝试试用版。 :) 试用是免费的。

以下是在 ASP.NET 中将 RTF 转换为 HTML 的代码示例:

    SautinSoft.RtfToHtml r = new SautinSoft.RtfToHtml();
    r.OutputFormat = SautinSoft.RtfToHtml.eOutputFormat.HTML_401;
    r.ImageStyle.IncludeImageInHtml = false; //To save images inside HTML as binary data specify this property to 'true'

    r.ImageStyle.ImageFolder = Server.MapPath(""); 
    r.ImageStyle.ImageSubFolder = "images";
    r.ImageStyle.ImageFileName = "picture";       

    string rtf = ".....";
    string html = r.ConvertString(rtf);        

    //show HTML
    if (html.Length>0)
    {
        Response.Buffer = true;
        Response.Clear();
        Response.ContentType = "text/html";
        Response.Write(html);
        Response.Flush();
        Response.End();
    }

Disclaimer: I'm working for this company.

As I see, the question is old but maybe someone search solution for this too. Our component RTF to HTML allows to convert RTF to HTML. You may download a component or try online-demo. Try the trial version first if you have a doubt. :) Trial is free.

Here's the code sample for the converting from RTF to HTML in ASP.NET:

    SautinSoft.RtfToHtml r = new SautinSoft.RtfToHtml();
    r.OutputFormat = SautinSoft.RtfToHtml.eOutputFormat.HTML_401;
    r.ImageStyle.IncludeImageInHtml = false; //To save images inside HTML as binary data specify this property to 'true'

    r.ImageStyle.ImageFolder = Server.MapPath(""); 
    r.ImageStyle.ImageSubFolder = "images";
    r.ImageStyle.ImageFileName = "picture";       

    string rtf = ".....";
    string html = r.ConvertString(rtf);        

    //show HTML
    if (html.Length>0)
    {
        Response.Buffer = true;
        Response.Clear();
        Response.ContentType = "text/html";
        Response.Write(html);
        Response.Flush();
        Response.End();
    }
来世叙缘 2025-01-05 04:56:25

唯一的问题是,当你在预算范围内工作时,额外的成本意味着更低的利润,所以我开始开发自己的版本。主要问题是,目前它仅支持粗体斜体,以及某些实体(&、©、®、&trade、& ;欧元和&###)并且缺乏字体和颜色支持,但它仍然是一项正在进行的工作。我正在添加字体和颜色,但令我头疼的是这些可能来自样式表而不是旧式的 html 标签。

上发布了启动代码

我正在 VB.NET 中编写此内容,并已在 CodeProject 启动代码

The only problem is when you work on a budget, additional costs mean lower profit, so I started to develop my own version. The main problem is that at the moment it only supports Bold and Italic, and certain entities (&, ©, ®, &trade, &euro and &###) and lacks both font and color support, but it is still a work in progress. I am adding font and color, but my headache is that these could come from stylesheets rather than the old fashion html tags.

I am writing this in VB.NET and have posted the startup code on CodeProject

Startup Code

岁月如刀 2025-01-05 04:56:25

如果你想以编程方式执行此操作,你应该解析你的 rtf (是一个简单的基于文本的文件),将 rtf 控制字转换为 html 标签。

在这里您可以找到 rtf 规格
http://www.biblioscape.com/rtf15_spec.htm

或使用现有的转换器:< a href="http://sourceforge.net/projects/rtf2html-lite/" rel="nofollow">http://sourceforge.net/projects/rtf2html-lite/

If you want to do it programattically you should parse your rtf (is a simple text based file), convert rtf control words to html tags.

Here you can find the rtf specs
http://www.biblioscape.com/rtf15_spec.htm

or use an already existing converter: http://sourceforge.net/projects/rtf2html-lite/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文