TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Getting RTF from WebView
Aman Kumar
Jan 18, 2016
7.7
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
In this blog you will learn how to Getting RTF from WebView.
CSharpRtfFromWebView.zip
Steps:
Create a blank windows store 8.1 app.
Add a WebView and RichEditBox in xaml of the page.
<WebView Height=
"500"
x:Name=
"webView"
Loaded=
"webView_Loaded"
NavigationCompleted=
"webView_NavigationCompleted"
Width=
"500"
HorizontalAlignment=
"Left"
/>
<RichEditBox x:Name=
"richEditBox"
Width=
"500"
Height=
"500"
HorizontalAlignment=
"Right"
/>
I have tried this for html string as it’s is easy to add script in html.
public
static
string
GetHTML()
{
return
"<html>"
+
"<head>"
+
"<script type='text/javascript'>"
+
"function select_body() "
+
"{"
+
"var range = document.body.createTextRange(); "
+
"range.select();"
+
"}"
+
"</script>"
+
"<body>"
+
"<h1>My First Heading</h1>"
+
"<p>My first paragraph.</p>"
+
"</body></head></html>"
;
}
Now navigate webview to the html string.
private
void
webView_Loaded(
object
sender, RoutedEventArgs e)
{
webView.NavigateToString(GetHTML());
}
When the webPage is navigated we can notify a script and get rtf from webview.
private async void webView_NavigationCompleted(WebView sender,
WebViewNavigationCompletedEventArgs args)
{
await webView.InvokeScriptAsync(
"select_body"
,
null
);
DataPackage p = await webView.CaptureSelectedContentToDataPackageAsync();
string
rtf = await p.GetView().GetRtfAsync();
richEditBox.Document.SetText(Windows.UI.Text.TextSetOptions.FormatRtf, rtf);
}
Getting RTF from WebView
Next Recommended Reading
Reduce CPU Usage and getting value from HashTable