2

I've got a WebView with some HTML content which I want to convert into RTF. I've looked at the RTF conversion functions out there and they all look a little flaky to be honest. So my idea is to copy content from the WebView into a RichEditBox, and save to RTF from there.

I've seen this example numerous times.

WebBrowser1.Document.ExecCommand("SelectAll", false, null);
WebBrowser1.Document.ExecCommand("Copy", false, null);

Unfortunately, WinRT's WebView control doesn't have a Document property, so I can't do this

Is there any way to pull the content from the control? To be clear, I don't want the HTML itself - I can get that already using

InvokeScript("eval", new string[] { "document.getElementById('editor').innerHTML;" });

What I want is the actual rendered HTML - the same as if I were to select everything in my WebView, press CTRL+C and then paste it into wordpad.

roryok
  • 9,325
  • 17
  • 71
  • 138

1 Answers1

3

This is part of a series of questions I asked in trying to accomplish a bigger task - converting HTML to RTF in a Windows Store App.

I'm delighted to report that the above can be done. I finally figured out how to do it, using DataPackage - normally used for sharing content between apps.

First, this javascript function must exist in the HTML loaded in the webview.

function select_body() {
    var range = document.body.createTextRange(); 
    range.select();
}

Next, you'll need to add using Windows.ApplicationModel.DataTransfer; to the top of your document. Not enough StackOverflow answers mention the namespaces used. I always have to go hunting for them.

Here's the code that does the magic:

// call the select_body function to select the body of our document
MyWebView.InvokeScript("select_body", null);

// capture a DataPackage object
DataPackage p = await MyWebView.CaptureSelectedContentToDataPackageAsync();

// extract the RTF content from the DataPackage
string RTF = await p.GetView().GetRtfAsync();

// SetText of the RichEditBox to our RTF string
MyRichEditBox.Document.SetText(Windows.UI.Text.TextSetOptions.FormatRtf, RTF);

I've spent about 2 weeks trying to get this to work. Its such a relief to finally discover I don't have to manually encode the file to RTF. Now if I can just get it to work the other way around, I'll be ecstatic. Not essential to the app I'm building, but it would be a lovely feature.

UPDATE

In retrospect you probably don't need to have the function in the HTML, you could probably get away with this (though I haven't tested):

MyWebView.InvokeScript("execScript", new string[] {"document.body.createTextRange().select();"})
Community
  • 1
  • 1
roryok
  • 9,325
  • 17
  • 71
  • 138
  • It does not work if images are contained in the HTML. Any ideas? – Shefali Mar 27 '14 at 09:38
  • it looks like there are some issues with images in a RichEditBox. See [here](http://social.msdn.microsoft.com/Forums/windowsapps/en-US/fd5e4f6e-3613-47bf-b57c-2aa19991e94d/saving-rtf-with-image-from-richeditbox-to-file?forum=winappswithcsharp) and [here](http://social.msdn.microsoft.com/Forums/windowsapps/en-US/540f2747-b350-4eac-930d-0af5ade88e8b/cant-save-images-in-richeditbox?forum=winappswithcsharp). You might be able to save it directly without going to the RichEditBox – roryok Mar 27 '14 at 16:51
  • Hi there, I'm very interested in your solution however, I don't understand how to inject the javascript in the html if I want a solution that allows me to download the content of any webpage. I mean, what is the alternative if the function does not exist in the page I want to copy ? – Marc Dec 09 '15 at 15:46
  • I was probably wrong that it needs to exist. I guess you could just use `InvokeScript` to run the function directly. see updated answer – roryok Dec 09 '15 at 16:28
  • Many many thanks for your quick update.. however my code always fails at the InvokeScript line with the exception "The method or operation is not implemented". Am I missing something ? – Marc Dec 09 '15 at 16:46
  • try eval instead of execScript? – roryok Dec 09 '15 at 16:49
  • I'm getting the same exception with eval.. Strange thing.. I'm in the context of trying to save news articles, and it fails whatever the webpage.. – Marc Dec 09 '15 at 16:54
  • Ok thank you I got it working with the help of msdn. I'm working on Windows 10 and there's a warning specifying that "InvokeScript may be altered or unavailable for releases after Windows 8.1. Instead, use InvokeScriptAsync." (https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.webview.invokescript.aspx). It worked fine with the InvokeScriptAsync method :) – Marc Dec 09 '15 at 17:15
  • ah yes of course, I forgot about that. did you use eval or execScript? – roryok Dec 09 '15 at 20:15
  • Hi , I am trying this solution to convert my html into rtf string. When try this I am getting this error "Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME)) " on InvokeScriptAsync method, which means the script is not correct. Can you please suggest me right script to select all content of webview. thanks inadvance. – Noorul Aug 23 '17 at 09:42
  • @Nooral try Marc's eval method above, see the link in his first comment – roryok Aug 24 '17 at 12:31