public Task PrintAsync(WebView viewToPrint, string jobName) { if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat) { IVisualElementRenderer existingRenderer = Platform.GetRenderer(viewToPrint); if ((existingRenderer ?? Platform.CreateRendererWithContext(viewToPrint, Settings.Context)) is IVisualElementRenderer renderer) { Android.Webkit.WebView droidWebView = renderer.View as Android.Webkit.WebView; if (droidWebView == null && renderer.View is WebViewRenderer xfWebViewRenderer) { droidWebView = xfWebViewRenderer.Control; } if (droidWebView != null) { droidWebView.Settings.JavaScriptEnabled = true; droidWebView.Settings.DomStorageEnabled = true; droidWebView.SetLayerType(Android.Views.LayerType.Software, null); // Only valid for API 19+ if (string.IsNullOrWhiteSpace(jobName)) { jobName = Forms9Patch.ApplicationInfoService.Name; } var printMgr = (PrintManager)Settings.Context.GetSystemService(Context.PrintService); printMgr.Print(jobName, droidWebView.CreatePrintDocumentAdapter(jobName), null); } if (existingRenderer == null) { renderer.Dispose(); } } } return(Task.CompletedTask); }
public Task PrintAsync(WebView viewToPrint, string jobName, FailAction failAction = FailAction.ShowAlert) { try { if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat) { IVisualElementRenderer existingRenderer = Platform.GetRenderer(viewToPrint); if ((existingRenderer ?? Platform.CreateRendererWithContext(viewToPrint, Settings.Context)) is IVisualElementRenderer renderer) { Android.Webkit.WebView droidWebView = renderer.View as Android.Webkit.WebView; if (droidWebView == null && renderer.View is WebViewRenderer xfWebViewRenderer) { droidWebView = xfWebViewRenderer.Control; } if (droidWebView != null) { droidWebView.Settings.JavaScriptEnabled = true; droidWebView.Settings.DomStorageEnabled = true; droidWebView.SetLayerType(Android.Views.LayerType.Software, null); // Only valid for API 19+ if (string.IsNullOrWhiteSpace(jobName)) { jobName = Forms9Patch.ApplicationInfoService.Name; } var printMgr = (PrintManager)Settings.Context.GetSystemService(Context.PrintService); printMgr.Print(jobName, droidWebView.CreatePrintDocumentAdapter(jobName), null); } if (existingRenderer == null) { renderer.Dispose(); } } } } catch (Exception e) { if (failAction == FailAction.ShowAlert) { Xamarin.Essentials.MainThread.BeginInvokeOnMainThread(async() => { using (var toast = Forms9Patch.Alert.Create("Print Failure", e.Message)) { await toast.WaitForPoppedAsync(); } }); } else if (failAction == FailAction.ThrowException) { throw e; } } return(Task.CompletedTask); }
async Task OnPageFinished(Android.Webkit.WebView webView, string jobName, PageSize pageSize, PageMargin margin, TaskCompletionSource <ToFileResult> taskCompletionSource) { if (string.IsNullOrWhiteSpace(jobName)) { jobName = Forms9Patch.ApplicationInfoService.Name; } var printMgr = (PrintManager)Settings.Context.GetSystemService(Context.PrintService); await Task.Delay(1000); // allow a bit more time for the layout to complete; printMgr.Print(jobName, webView.CreatePrintDocumentAdapter(jobName), null); _activityIndicatorPopup.Dispose(); _activityIndicatorPopup = null; taskCompletionSource.SetResult(new ToFileResult(false, jobName)); webView.Dispose(); }
public override bool PrintDocumentFromWebView(object webView) { try { PrintManager printManager = (PrintManager)Forms.Context.GetSystemService(Context.PrintService); Android.Webkit.WebView platformWebView = (Android.Webkit.WebView)(webView as CustomWebView).PlatformControl; var printAttributes = new Android.Print.PrintAttributes.Builder().Build(); printManager.Print("BodyReportJob", platformWebView.CreatePrintDocumentAdapter("BodyReportJob"), printAttributes); return(true); } catch (Exception) { return(false); } }
public void print(string content) { try { var printManager = (Android.Print.PrintManager) this.ApplicationContext.GetSystemService(Context.PrintService); //var browser = new WebView(); Android.Webkit.WebView s = new Android.Webkit.WebView(this.ApplicationContext); //wv.CreatePrintDocumentAdapter() //var htmlSource = new HtmlWebViewSource(); //htmlSource.Html = content; // htmlSource.Html = @"<html><body> //<h1>Xamarin.Forms</h1> //<p>Welcome to WebView.</p> //</body></html>"; //WebView s = new WebView(this); s.LoadData(content, "text/html", ""); //browser.Source = htmlSource; printManager.Print("testm", s.CreatePrintDocumentAdapter("pipi"), null); } catch (Exception ex) { } }