Whats is the best way to load a pdf on server in the xamarin pcl App for both Ios and Android. Is there a good nuget or we have to write the custom renderers?
3 Answers
Opening a PDF in the app, you have a few options.
iOS has had PDF support in its WebView for a while. Android doesn't. If you want to do it in a WebView you can do this:
var webView = new WebView();
webView.Source = new UrlWebViewSource() { Url = "https://example.com/my.pdf" };
If you are only supporting Android API 21+ and higher, you can use the PdfRenderer for Android only.
If you need to support older devices, or want a cross platform approach, you have 2 options. If its a public PDF document, you can take an easy approach and use Google's PDF Viewer
var webView = new WebView();
var pdfUrl = "https://example.com/my.pdf";
var googleUrl = "http://drive.google.com/viewerng/viewer?embedded=true&url=";
webView.Source = new UrlWebViewSource() { Url = googleUrl + pdfUrl };
If it's secure and you have to download it to your app first, use Mozilla's PDFJS Viewer. Xamarin have a walkthrough here: https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/controls/display-pdf/

- 16,089
- 6
- 66
- 109
-
Nice! Did not know later versions of Android support it. – hvaughan3 Apr 28 '17 at 02:45
-
1I'm actually just going to try and confirm that. I know that they included PdfRenderer in API21+. But I was sure it could have a PDF in the WebView in recent versions, though I always did an internal PDF reader anyway, because I had to support down to API 15. – Adam Apr 28 '17 at 03:19
-
1@hvaughan3 - my bad, just tested wtih 7.0 and no PDF loading in a WebView. :( I was thinking of the PdfRenderer. Thanks for pointing it out. – Adam Apr 28 '17 at 03:25
-
@AdamPedley have you found a solution? With webView.Source = new UrlWebViewSource() { Url = "https://example.com/my.pdf" }; I see nothing... – Alessandro Caliaro Nov 29 '17 at 09:19
-
@AlessandroCaliaro - use the Mozilla PDFJS approach if you want a cross platform solution in a webview. – Adam Dec 05 '17 at 03:02
-
@AdamPedley I have tried (not too much...) with PDFJS but I had some compilation problems.... with Assets. I try again. Thanks – Alessandro Caliaro Dec 05 '17 at 11:18
-
The Xamarin guide talks about displaying a local pdf file. But how do we download the pdf file to our app locally in the first place? – jbyrd Dec 11 '17 at 22:29
Like Tim said, on iOS you can use a WebView
pointed to the remote PDF and get on with your life.
On Android the WebView cannot handle PDFs by default (W.T.F!!).
So that means you must do one of the following:
Use the remote PDF:
- use Google Drives URL for it
https://drive.google.com/viewerng/viewer?url=<PDF URL HERE>
- use Google Drives URL for it
Download the PDF locally:
- Use PDF.js to display it in a WebView (Xamarin Forms link)
- Use an
Intent
on Android to ask Android what apps the user has installed already that can handle PDFs (SO link)
I'm not aware of any existing Xamarin Forms PDF plugins that will do this for you.
You will have to resort to custom renderers to achieve this. Unless it is okay to open the remote PDF in a browser instead of in your app. In that case you could use Device.OpenUri(...)
to open it.
Another option would be to incorporate a WebView in your Xamarin Forms app, and open the PDF in there. That would prevent users from actually leaving the app when they open the PDF.

- 2,834
- 14
- 20