2

I am trying to make simple application to test printing a WebView page.

Below is my simple code

WebView webView = new WebView(this);
webView.LoadUrl("https://www.google.com");
PrintDocumentAdapter adapter = webView.CreatePrintDocumentAdapter("test");
PrintManager printMgr = (PrintManager)GetSystemService(PrintService);
printMgr.Print("printTest", adapter, null);
myWebView = webView;

However, it is generating an exception in WebView.CreatePrintDocumentAdapter("test") method, which is written below.

Java.Lang.NoSuchMethodError: no method with name='createPrintDocumentAdapter' signature='(Ljava/lang/String;)Landroid/print/PrintDocumentAdapter;' in class Landroid/webkit/WebView;

Note that I have included internet and network state permission in Android manifest. What is causing this? How could I create print feature if this exception blocks it?

The device that I am using is Samsung SM-G7102 (Android 4.4 - API 19)

Any helps would be appreciated.

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
Darren Christopher
  • 3,893
  • 4
  • 20
  • 37

1 Answers1

5

CreatePrintDocumentAdapter(String documentName) was added in API 21.

You can do an VERSION.SdkInt check at runtime and call the appropriate method:

if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop) 
    adapter = webView.CreatePrintDocumentAdapter("test");
 else 
    adapter = webView.CreatePrintDocumentAdapter();

re: https://developer.android.com/reference/android/print/PrintDocumentAdapter.html

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • I could not add those, the `else` block complains that `WebView.CreatePrintDocumentAdapter() is obsolete: 'deprecated'`. Any solution for this? – Darren Christopher Mar 16 '17 at 04:55
  • 1
    @DarrenChristopher Check what your project/application API levels are set to: https://developer.xamarin.com/guides/android/application_fundamentals/understanding_android_api_levels/ – SushiHangover Mar 16 '17 at 04:58
  • I just changed the Compile Android version to Android 4.4 and leave the others as default. Now the if block is the one complains. How to overcome this version issue? – Darren Christopher Mar 16 '17 at 05:02
  • Set your `Target Framework` to the highest API level that you wish to support at compile time, set the `Minimum Android Version` to the lowest level that you wish to support at runtime, and set the `Target Android Version` to the highest level that is supported at runtime... i.e: something like: `Target Framework` ***and*** `Target Android Version` set to `7.1` and `Minimum Android Version` set to `4.0` – SushiHangover Mar 16 '17 at 05:09
  • Thanks! It solved the version issue very well. However, when I tested it and printed as pdf, I checked the page is blank. I got `[WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation` in the output, but according to [this](http://stackoverflow.com/questions/20557708/pac-support-disabled-because-there-is-no-system-implementation), it does not affect anything. What do you think the problem is? – Darren Christopher Mar 16 '17 at 06:09
  • Is your `WebView` visible and is the Google page rendered before executing the `Print` – SushiHangover Mar 16 '17 at 06:20
  • I am now implementing the `WebViewClient` class, override the `OnPageFinished` method and set it as my `WebView`'s client. It works well now! Thank you! – Darren Christopher Mar 16 '17 at 06:53