C# 将 Rtf 转换为 HTML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/439301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Rtf to HTML
提问by Aaron Smith
We have a crystal report that we need to send out as an e-mail, but the HTML generated from the crystal report is pretty much just plain ugly and causes issues with some e-mail clients. I wanted to export it as rich text and convert that to HTML if it's possible.
我们有一个需要作为电子邮件发送的水晶报告,但是从水晶报告生成的 HTML 非常难看,并且会导致某些电子邮件客户端出现问题。我想将其导出为富文本,并在可能的情况下将其转换为 HTML。
Any suggestions?
有什么建议?
采纳答案by Richard R
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.
我会在 CodeProject RTFConverter 上查看这个工具。这家伙详细介绍了程序的工作原理以及转换的详细信息。
回答by Andrew Hare
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?
我不知道有任何库可以做到这一点(但我相信有很多库可以)但是如果您已经可以从水晶报告中创建 HTML,为什么不使用 XSLT 来清理标记?
回答by chakrit
I think you can load it in a Word document object by using .NET office programmability support and Visual Studio tools for office.
我认为您可以使用 .NET office 可编程性支持和 Visual Studio 办公工具将其加载到 Word 文档对象中。
And then use the document instance to re-save as an HTML document.
然后使用文档实例重新保存为 HTML 文档。
I am not sure how but I believe it is possible entirely in .NET without any 3rd party library.
我不确定如何,但我相信在没有任何 3rd 方库的情况下完全可以在 .NET 中使用。
回答by stiduck
You can try to upload it to google docs, and download it as HTML.
您可以尝试将其上传到 google docs,然后将其下载为 HTML。
回答by Kluge
If you don't mind getting your hands dirty, it isn't that difficult to write an RTF to HTML converter.
如果您不介意弄脏自己的手,那么编写一个 RTF 到 HTML 转换器并不难。
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.
编写一个通用的 RTF->HTML 转换器会有些复杂,因为您需要处理数百个 RTF 动词。但是,在您的情况下,您只处理 Crystal Reports 专门使用的那些动词。我敢打赌,由 Crystal 生成的标准 RTF 编码不会因报告而异。
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.
我用 C++ 编写了一个 RTF 到 HTML 转换器,但它只处理基本格式,如字体、段落对齐等。我的翻译器基本上去掉了它不准备处理的任何特殊格式。它花了大约 400 行 C++。它基本上扫描文本中的 RTF 标签,并用等效的 HTML 标签替换它们。不在我的列表中的 RTF 标签被简单地去除了。编写这样的转换器时,正则表达式函数真的很有帮助。
回答by missaghi
Mike Stall posted the code for one he wrote in c# here :
Mike Stall 在这里发布了他用 C# 编写的代码:
http://blogs.msdn.com/jmstall/archive/2006/10/20/rtf_5F00_html.aspx
http://blogs.msdn.com/jmstall/archive/2006/10/20/rtf_5F00_html.aspx
回答by Matthew Manela
There is also a sample on the MSDN Code Samples gallery called Converting between RTF and HTMLwhich allows you to convert between HTML, RTF and XAML.
MSDN 代码示例库上还有一个示例,称为在 RTF 和 HTML 之间转换,它允许您在 HTML、RTF 和 XAML 之间进行转换。
回答by Max von Hippel
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:
我回到家并尝试了下面的代码,但它不起作用。对于任何想知道的人来说,剪贴板不仅仅是像我希望的那样神奇地转换东西。相反,它允许应用程序“上传”具有各种粘贴格式的数据对象,然后您粘贴(在我的比喻中将是“下载”)被粘贴的程序指定其首选格式。我个人最终使用了之前推荐的这个代码,它非常易于使用且非常有效。导入代码(在 VStudio 中,项目 -> 添加现有文件)后,您只需像这样将 html 转到 rtf:
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.
同样,不完全确定这是否可行,但只是在我的 iPhone 上弄乱了一些 html,我怀疑它会起作用。文档在这里。更深入的解释/文档 RE 可以在此处找到剪贴板中数据模型的获取和设置。
(Yes I am fully aware I'm here years later, but I assume this question is one which some people still want answered).
(是的,我完全知道我多年后还在这里,但我认为这个问题是一些人仍然想要回答的问题)。