Rtf2Html
Rtf2Html copied to clipboard
Converting Rtf via Xaml to Html including images
Rtf2Html
Convert Rtf via Xaml to Html including images. The code is adapted from Matthew Manelas Codesample with a few tweaks:
-
Usage of
RichTextBoxis avoided so we do not need to work around STA issues. Instead we useTextEditorCopyPaste.ConvertRtfToXamlvia reflection. This is basically the same as usingTextRangeBase.Save(..., DataFormats.XamlPackage)withRichTextBoxbut feels a bit cleaner to me. -
Added support for processing the zipped
XamlPackagestream including images. RequiresZipArchivefromSystem.IO.Compressionin .NET 4.5
Simple conversion between Rtf and Xaml is also included by just exposing Microsofts internal implementations.
Examples
Rtf to Html (including image content):
var rtf = File.ReadAllText("doc.rtf");
var htmlResult = RtfToHtmlConverter.RtfToHtml(rtf, "doc");
htmlResult.WriteToFile("doc.html");
Rtf to Xaml (without content):
var rtf = File.ReadAllText("doc.rtf");
var xaml = RtfToXamlConverter.RtfToXaml(rtf);
File.WriteAllText("doc.xaml", xaml);
Rtf to Xaml package (zip with content)
var rtf = File.ReadAllText("doc.rtf");
using (var xamlStream = RtfToXamlConverter.RtfToXamlPackage(rtf))
File.WriteAllBytes("doc.xap", xamlStream.ToArray());
Rtf to plain text
var rtf = File.ReadAllText("doc.rtf");
var plainText = RtfToPlaintTextConverter.RtfToPlainText(rtf);
File.WriteAllText("doc.txt", plainText);