1

I'm currently trying to port Matthew Manela's "Converting between RTF and XAML" code sample to WinRT

I've got the HTML to XAML code working, but I've hit a snag when getting it into a RichEditBox.

Matthew's code is WPF based, and uses the following function to convert XAML to RTF.

private static string ConvertXamlToRtf(string xamlText) 
{ 
    var richTextBox = new RichTextBox(); 
    if (string.IsNullOrEmpty(xamlText)) return ""; 
    var textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); 
    using (var xamlMemoryStream = new MemoryStream()) 
    { 
        using (var xamlStreamWriter = new StreamWriter(xamlMemoryStream)) 
        { 
            xamlStreamWriter.Write(xamlText); 
            xamlStreamWriter.Flush(); 
            xamlMemoryStream.Seek(0, SeekOrigin.Begin); 
            textRange.Load(xamlMemoryStream, DataFormats.Xaml); 
        } 
    } 
    using (var rtfMemoryStream = new MemoryStream()) 
    { 
        textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); 
        textRange.Save(rtfMemoryStream, DataFormats.Rtf); 
        rtfMemoryStream.Seek(0, SeekOrigin.Begin); 
        using (var rtfStreamReader = new StreamReader(rtfMemoryStream)) 
        { 
            return rtfStreamReader.ReadToEnd(); 
        } 
    } 
}

I've tried rewriting this in WinRT using RichEditBox, but come up against some issues. Most noteably, WPF TextRange accepts a XAML dataformat, but WinRT ITextRange doesn't have this. However, I know that I can inject XAML directly into a RichTextBlock control.

Is there any way to copy the text from a RichTextBlock and paste it into a RichEditBox, programmatically?

OR, failing that, is there a way to convert HTML to RTF that works in WinRT / Windows Store Apps?

roryok
  • 9,325
  • 17
  • 71
  • 138
  • Only a comment. Not the same problem but may shed some light. http://underground.infovark.com/2011/03/03/highlighting-query-terms-in-a-wpf-textblock/ – paparazzo Feb 24 '14 at 15:35
  • Again not the same but may help http://stackoverflow.com/questions/3728584/how-to-display-search-results-in-a-wpf-items-control-with-highlighted-query-term – paparazzo Feb 24 '14 at 15:45
  • Thanks, I don't have a problem highlighting stuff though, this is easy enough in either RichEditBox or RichTextBlock. I just can't move the content from one to the other. – roryok Feb 24 '14 at 15:59
  • [Check this sample out](http://code.msdn.microsoft.com/windowsapps/Html-to-RTF-a757fac5). It describes how to convert HTML to RTF, but using the RichTextBlock. You can try and use his methods though. Another sample is [here](http://code.msdn.microsoft.com/windowsapps/RTF-TO-HTML-TO-RTF-in-11ec0bd7) and seems to be more what you are requiring. – Nate Diamond Feb 24 '14 at 22:43
  • You may also want to look into the [HtmlAgilityPack](http://htmlagilitypack.codeplex.com). – Nate Diamond Feb 24 '14 at 22:56
  • Thanks Nate, the first sample you've linked uses XAML injection with the RichTextBlock, which I can already do. It's the next stage I have an issue with. The second sample technically works but is quite buggy and doesn't offer any control over the RTF – roryok Feb 25 '14 at 11:30

1 Answers1

1

I got my answer from Rob Caplan on the msdn Windows Apps Forum

http://social.msdn.microsoft.com/Forums/windowsapps/en-US/c5f4e679-c563-463c-b812-05b16cd5720f/converting-html-to-rtf-using-richeditbox-and-richtextbox?forum=winappswithcsharp

Direct Quote:

"You'll have to do the conversion yourself. The RichEdiBox doesn't have any native conversion ability. It renders RTF directly and does not convert it to Xaml. This is different from the WPF implementation, which converts RTF rather than displaying it directly."

Well, that's that then. I'm currently adapting an open source RTF Library for WinRT usage. I'll post when done.

EDIT

Rob was wrong! It is possible, albeit via a workaround. I figured out how to do it using the DataPackage class. Full answer here: https://stackoverflow.com/a/22093837/352867

Community
  • 1
  • 1
roryok
  • 9,325
  • 17
  • 71
  • 138
  • It does not work if images are contained in the HTML. – Shefali Mar 27 '14 at 09:27
  • @Shefali I guess it depends on where the images come from, and if they have absolute paths etc. It may be possible to convert relative paths in the src to absolute paths and then copy – roryok Aug 19 '15 at 08:06