Convert Rtf via Xaml to Html including images. The code is adapted from Matthew Manelas Codesample with a few tweaks:
-
Usage of
RichTextBox
is avoided so we do not need to work around STA issues. Instead we useTextEditorCopyPaste.ConvertRtfToXaml
via reflection. This is basically the same as usingTextRangeBase.Save(..., DataFormats.XamlPackage)
withRichTextBox
but feels a bit cleaner to me. -
Added support for processing the zipped
XamlPackage
stream including images. RequiresZipArchive
fromSystem.IO.Compression
in .NET 4.5
Simple conversion between Rtf
and Xaml
is also included by just exposing Microsofts internal implementations.
var rtf = File.ReadAllText("doc.rtf");
var htmlResult = RtfToHtmlConverter.RtfToHtml(rtf, "doc");
htmlResult.WriteToFile("doc.html");
var rtf = File.ReadAllText("doc.rtf");
var xaml = RtfToXamlConverter.RtfToXaml(rtf);
File.WriteAllText("doc.xaml", xaml);
var rtf = File.ReadAllText("doc.rtf");
using (var xamlStream = RtfToXamlConverter.RtfToXamlPackage(rtf))
File.WriteAllBytes("doc.xap", xamlStream.ToArray());
var rtf = File.ReadAllText("doc.rtf");
var plainText = RtfToPlaintTextConverter.RtfToPlainText(rtf);
File.WriteAllText("doc.txt", plainText);