将 Rtf 转换为 HTML

发布于 2024-07-11 21:45:31 字数 1542 浏览 6 评论 0原文

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

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

发布评论

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

评论(8

〃安静 2024-07-18 21:45:31

我会在 CodeProject RTFConverter 上查看这个工具。 这个人详细介绍了程序的工作原理以及转换的细节。

编写您自己的 RTF 转换器

I would check out this tool on CodeProject RTFConverter. This guy gives a great breakdown of how the program works along with details of the conversion.

Writing Your Own RTF Converter

似狗非友 2024-07-18 21:45:31

There is also a sample on the MSDN Code Samples gallery called Converting between RTF and HTML which allows you to convert between HTML, RTF and XAML.

顾挽 2024-07-18 21:45:31

更新:

我回到家并尝试了下面的代码,但它不起作用。 对于任何想知道的人来说,剪贴板并不只是像我希望的那样神奇地转换内容。 相反,它允许应用程序使用各种粘贴格式“上传”数据对象,然后粘贴(在我的比喻中是“下载”)要粘贴的程序指定其首选格式。 我个人最终使用了此代码,之前已经推荐过,它非常容易使用并且非常有效。 导入代码后(在 VStudio 中,项目 -> 添加现有文件),然后只需将 html 转为 rtf,如下所示:

return HtmlToRtfConverter.ConvertHtmlToRtf(myRtfString);

或相反的方向:(

return RtfToHtmlConverter.ConvertHtmlToRtf(myHtmlString);

下面是我之前的错误答案,以防有人对时间顺序感兴趣这个答案哈哈)

大多数(如果不是全部)上述答案都为当前的问题提供了全面的、通常基于库的解决方案。
我离开了我的电脑,因此无法测试这个想法,但另一种廉价且隐晦的黑客方法如下。

private string HTMLFromRtf(string rtfString)
{
            Clipboard.SetData(DataFormats.Rtf, rtfString);
            return Clipboard.GetData(DataFormats.Html);         
}

再说一次,我不完全确定这是否可行,但只是在我的 iPhone 上摆弄一些 html,我怀疑它会。 文档位于此处。 更深入的解释/文档有关剪贴板中数据模型的获取和设置可以找到 此处

(是的,我完全知道几年后我在这里,但我认为这个问题是一些人仍然想要回答的问题)。

UPDATED:

I got home and tried the below code and it does not work. For anyone wondering, the clipboard does not just magically convert stuff like I'd hoped. Rather, it allows an application to sort of "upload" a data object with a variety of paste formats, and then then you paste (which in my metaphor would be the "download") the program being pasted into specifies its preferred format. I personally ended up using this code, which has been recommended previously, and it was enormously easy to use and very effective. After you have imported the code (in VStudio, Project -> Add Existing Files) you then just go html to rtf like this:

return HtmlToRtfConverter.ConvertHtmlToRtf(myRtfString);

or the opposite direction:

return RtfToHtmlConverter.ConvertHtmlToRtf(myHtmlString);

(below is my previous incorrect answer, in case anyone is interested in the chronology of this answer haha)

Most if not all of the above answers provide comprehensive, often Library-based solutions to the problem at hand.
I am away from my computer and thus cannot test the idea, but one alternative, cheap and vaguely hack-y method would be the following.

private string HTMLFromRtf(string rtfString)
{
            Clipboard.SetData(DataFormats.Rtf, rtfString);
            return Clipboard.GetData(DataFormats.Html);         
}

Again, not totally sure if this would work, but just messing around with some html on my iPhone I suspect it would. Documentation is here. More in depth explanation/docs RE the getting and setting of data models in the clipboard can be found here.

(Yes I am fully aware I'm here years later, but I assume this question is one which some people still want answered).

情丝乱 2024-07-18 21:45:31

如果您不介意亲自动手,那么编写 RTF 到 HTML 转换器并不困难。

编写通用 RTF->HTML 转换器会有些复杂,因为您需要处理数百个 RTF 动词。 但是,在您的情况下,您只处理 Crystal Reports 专门使用的那些动词。 我敢打赌,Crystal 生成的标准 RTF 编码在不同的报告中不会有太大差异。

我用 C++ 编写了一个 RTF 到 HTML 转换器,但它只处理基本格式,如字体、段落对齐等。我的翻译器基本上删除了它不准备处理的任何特殊格式。 大约用了 400 行 C++ 代码。 它基本上扫描文本中的 RTF 标签,并将其替换为等效的 HTML 标签。 不在我的列表中的 RTF 标签将被简单地删除。 在编写这样的转换器时,正则表达式函数非常有用。

If you don't mind getting your hands dirty, it isn't that difficult to write an RTF to HTML converter.

Writing a general purpose RTF->HTML converter would be somewhat complicated because you would need to deal with hundreds of RTF verbs. However, in your case you are only dealing with those verbs used specifically by Crystal Reports. I'll bet the standard RTF coding generated by Crystal doesn't vary much from report to report.

I wrote an RTF to HTML converter in C++, but it only deals with basic formatting like fonts, paragraph alignments, etc. My translator basically strips out any specialized formatting that it isn't prepared to deal with. It took about 400 lines of C++. It basically scans the text for RTF tags and replaces them with equivalent HTML tags. RTF tags that aren't in my list are simply stripped out. A regex function is really helpful when writing such a converter.

何必那么矫情 2024-07-18 21:45:31

我认为您可以使用 .NET Office 可编程性支持和 Visual Studio Office 工具将其加载到 Word 文档对象中。

然后使用文档实例重新保存为 HTML 文档。

我不知道如何实现,但我相信在 .NET 中完全可以实现,无需任何第三方库。

I think you can load it in a Word document object by using .NET office programmability support and Visual Studio tools for office.

And then use the document instance to re-save as an HTML document.

I am not sure how but I believe it is possible entirely in .NET without any 3rd party library.

我的痛♀有谁懂 2024-07-18 21:45:31

我不知道有任何库可以执行此操作(但我确信有很多库可以),但是如果您已经可以从水晶报表创建 HTML,为什么不使用 XSLT 来清理标记呢?

I am not aware of any libraries to do this (but I am sure there are many that can) but if you can already create HTML from the crystal report why not use XSLT to clean up the markup?

蝶舞 2024-07-18 21:45:31

您可以尝试将其上传到 google docs,然后将其下载为 HTML。

You can try to upload it to google docs, and download it as HTML.

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