5

I want to print a WebView contents inside a UWP app.

I've set up my WebView to accept an HTML string, and this works fine:

    <WebView
        ext:HtmlExtension.HtmlString="{Binding HtmlString}"
        x:Name="MyWebView"
        Grid.Row="1"
        Grid.Column="0"
    />

I noticed this though on MSDN:

A WebView that hosts content off the UI thread is not compatible with parent controls that require gestures to propagate up from the WebView control to the parent, such as FlipView, ScrollViewer, and other related controls. These controls will not be able to receive gestures initiated in the off-thread WebView. In addition, printing off-thread web content is not directly supported – you should print an element with WebViewBrush fill instead.

Now I'm completely confused.

Can someone please explain how I can:

  • Create a WebViewBrush given I have a WebView
  • How to then print this WebViewBrush

Any help is greatly appreciated. Thanks.

b85411
  • 9,420
  • 15
  • 65
  • 119

1 Answers1

5

You can use the following codes to create a WebViewBrush:

WebViewBrush wvBrush = new WebViewBrush();
wvBrush.SetSource(myWebView);
wvBrush.Redraw();
myRect.Fill = wvBrush;

XAML:

<WebView Name="myWebView" Source="http://www.google.com" Width="1024" Height="500" LoadCompleted="myWebView_LoadCompleted"/>
<Rectangle Name="myRect" Width="1024" Height="500"/>

Then the static content of the webview will be rendered into the Rechtangle just like a picture. And you can print the Rechtangle instead.

For printing, I suggest you reading through Print from your app. And there is also an official sample for you to refer to:UWP Print Sample.

Elvis Xia - MSFT
  • 10,801
  • 1
  • 13
  • 24
  • Hi there and thank you very much for your reply. I finally figured out how to get the Brush working, I have yet to get the printing working though (I have tried the print sample you linked to - but I am running into problems printing the `FrameworkElement` where the Brushes are because the sample seems to be printing XAML `Page` objects). I have put up a message for the printing problem at http://stackoverflow.com/questions/39033318/how-to-save-webviewbrush-as-image-uwp-universal - hopefully if you get a chance you might be able to shed some light? Thanks – b85411 Aug 19 '16 at 08:02
  • 1
    Hi, I know time has passed, but I have been through the pain of Print Manager and helpers in UWP printing. There are such serious limitations with UWP printing that I am now moving all my reports to Print from the browser directly, I do use the webview for in-app viewing. – 27k1 Aug 04 '18 at 13:27