C# Corner
Tech
News
Videos
Forums
Trainings
Books
Events
More
Interviews
Jobs
Live
Learn
Career
Members
Blogs
Challenges
Certifications
Bounties
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Getting RTF from WebView
WhatsApp
Aman Kumar
9y
8
k
0
0
25
Blog
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);
}
People also reading
Membership not found