Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Problem With Printing WebView in UWP Phone

$
0
0

Unable to print WebView in xamarin forms.
I am making use of dependency service to print webview, it works fine in android and iOS but somehow page get cropped in UWP.
Following is the code and screenshot for the same.
Any help is appreciated.

         public class Print_UWP : IPrint
            {
                PrintManager printmgr = PrintManager.GetForCurrentView();
                PrintDocument PrintDoc = null;
                PrintDocument printDoc;
                PrintTask Task = null;
                Windows.UI.Xaml.Controls.WebView ViewToPrint = new Windows.UI.Xaml.Controls.WebView();
                public Print_UWP()
                {
                    printmgr.PrintTaskRequested += Printmgr_PrintTaskRequested;
                }

                public async void PrintUWpAsync(string htmlSource)
                {
                    ViewToPrint.NavigateToString(htmlSource);
                    if (PrintDoc != null)
                    {
                        printDoc.GetPreviewPage -= PrintDoc_GetPreviewPage;
                        printDoc.Paginate -= PrintDoc_Paginate;
                        printDoc.AddPages -= PrintDoc_AddPages;
                    }
                    this.printDoc = new PrintDocument();
                    try
                    {
                        printDoc.GetPreviewPage += PrintDoc_GetPreviewPage;
                        printDoc.Paginate += PrintDoc_Paginate;
                        printDoc.AddPages += PrintDoc_AddPages;

                        bool showprint = await PrintManager.ShowPrintUIAsync();

                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e.ToString());
                    }
                    PrintDoc = null;
                    GC.Collect();

                }


                private void Printmgr_PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
                {
                    var deff = args.Request.GetDeferral();
                    Task = args.Request.CreatePrintTask("Invoice", OnPrintTaskSourceRequested);

                    deff.Complete();

                }
                async void OnPrintTaskSourceRequested(PrintTaskSourceRequestedArgs args)
                {
                    var def = args.GetDeferral();
                    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        args.SetSource(printDoc.DocumentSource);
                    });
                    def.Complete();
                }

                private void PrintDoc_AddPages(object sender, AddPagesEventArgs e)
                {
                    printDoc.AddPage(ViewToPrint);
                    printDoc.AddPagesComplete();
                }

                private void PrintDoc_Paginate(object sender, PaginateEventArgs e)
                {
                    PrintTaskOptions opt = Task.Options;
                    printDoc.SetPreviewPageCount(1, PreviewPageCountType.Final);
                }

                private void PrintDoc_GetPreviewPage(object sender, GetPreviewPageEventArgs e)
                {
                    printDoc.SetPreviewPage(e.PageNumber, ViewToPrint);
                }
        }

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>