Print from Xamarin.forms Webview

Please help me.

I am creating a Xamarin.forms WebView app for Android and iOS, UWP. I need printing functionality in this app.

I can call C# functions from my webview. Basically i do this by redirecting to a new page in webview which has some text like "abc".
And onNavigated event will see if "abc" is there in the url.. If "abc" is there then it should call xyz() function..

But my problem is once the function is executed, how do i print the webview page? MY WEBVIEW CODE has a script which calls window.print() but running it on iOS does NOTHING. EDIT (10/6/19): UWP and android also nothing happen.

How can i make it work on iOS, uwp and android?

Just help me with the code which I can paste it in xyz() function or if i have to create new function.
VIVAAN mathur

Best Answer


  • ColeXColeX Member, Xamarin Team Xamurai
    edited June 2019

    There is no such function in Forms , you have to use Dependency service , implement it on each platform.

    Solution for Android and iOS :

    UWP :

  • vivaanmathurvivaanmathur Member ✭✭
    Can’t you tell me how to. I don’t understand
  • ColeXColeX Member, Xamarin Team Xamurai

    Did you download the sample and check it ?

  • vivaanmathurvivaanmathur Member ✭✭
    Yes cloned it but didn’t get it.. don’t know where to install what packages and all
  • vivaanmathurvivaanmathur Member ✭✭
    Thanks. It doesn’t have uwp
  • ColeXColeX Member, Xamarin Team Xamurai

    Yes, you have to integrate UWP into project by yourself ,i already attached UWP print webview native code ,just like the way implemented on ios and android , create Dependency service .

  • ColeXColeX Member, Xamarin Team Xamurai

    Try first, if have any problem, go back here .

  • BuildCalcBuildCalc USMember ✭✭✭

    I so wanted to do this but it was too hard. Finally built it into Forms9Patch.

    Verifying that Printing is available

    Before printing, you should verify that printing is available on your device. To do so, call:

    if (Forms9Patch.PrintService.CanPrint)
        // do the printing here

    Print the contents of a Xamarin.Forms.WebView

    using Forms9Patch;
        var myWebView = new Xamarin.Forms.WebView
        myWebView.Source = new HtmlWebViewSource 
            Html = "some HTML text here" 

    Note that your WebView does not have to be attached to a Layout. This allows you to Print without having to display the WebView in your app’s UI.

    Printing an HTML string

    using Forms9Patch;
        var myHtmlString =  @"
                                <!DOCTYPE html>
                                <h1>Convert to PNG</h1>
                                <p>This html will be converted to a PNG, PDF, or print.</p>

    PLEASE NOTE: iOS sometimes places the page breaks in weird places. I have a StackOverflow Bounty on why this happens and how to fix it.

    Using EmbeddedResource as a source for a Xamarin.Forms.WebView

    This is sort of an experimental feature I’ve built that I’ve found it useful. As such the documentation is sparse. It allow you to put HTML content in a folder in your app’s EmbeddedResources folder and then use it as a source for a WebView. A much nicer solution than using platform specific approach provided by Xamarin. It also supports putting all of the HTML content into a zip file. Please take a look at the source code to see how it works.

Sign In or Register to comment.