best way to open a remote pdf in xamarin forms pcl project

纵饮孤独 提交于 2019-12-10 17:16:07

问题


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?


回答1:


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/




回答2:


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:

  1. Use the remote PDF:

    • use Google Drives URL for it https://drive.google.com/viewerng/viewer?url=<PDF URL HERE>
  2. 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)



回答3:


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.



来源:https://stackoverflow.com/questions/43666292/best-way-to-open-a-remote-pdf-in-xamarin-forms-pcl-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!