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

Printing a form

$
0
0

I've been trying for hours to send a form to a printing service for both Android and iOS, something I have absolutely no experience with. I've seen https://forums.xamarin.com/discussion/42417/printing-on-ipad-from-webview which, I thought, sent me on the right path but this prints a WebView control. In my case, I want to print a ContentPage.

As an alternative, I thought of creating a HtmlWebViewSource that would generate the equivalent view of the information I want to print, but again, it doesn't work. My main issue is to go from Xamarin.Forms.WebView to (lets say) Android.Webkit.WebView to access the CreatePrintDocumentAdapter()

In my form, I have:

private void OnPrint()
{
  var WebViewSource = new HtmlWebViewSource()
  {
    Html =
    "<html>" +
    "<body>" +
    "Hello world" +
    "</body>" +
    "</html>"
  };

  var WebView = new WebView() { Source = WebViewSource };
  DependencyService.Get<IPrintService>().Print(WebView);
}

IPrintService is

public interface IPrintService
{
  void Print(WebView webView);
}

and my DependencyService is

[assembly: Dependency(typeof(PrintService))]
namespace Whatever.ApplicationLayer.Droid
{
  public class PrintService : IPrintService
  {
    public void Print(WebView webView)
    {
      var PrintManager = (PrintManager)Forms.Context.GetSystemService(Context.PrintService);
      var AndroidWebView = (Android.Webkit.WebView)webView.On<Xamarin.Forms.PlatformConfiguration.Android>();
      var JobName = "Test";
      PrintManager.Print(JobName, AndroidWebView.CreatePrintDocumentAdapter(JobName), null);
    }
  }
}

When it's time to cast webView, it dies ("System.InvalidCastException: Specified cast is not valid.") What am I doing wrong? What am I missing?


Viewing all articles
Browse latest Browse all 91519

Trending Articles



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