Xamarin.forms display PDF in webview not working

后端 未结 2 1902
粉色の甜心
粉色の甜心 2020-12-12 04:10

I download a pdf stream from my server. In my app I save the bytearray to a the local folder as pdf. But when I open it in the webview, it just shows a white page.

I

相关标签:
2条回答
  • 2020-12-12 04:37

    Following approach will allow you to view pdf in xamarin forms. If you are trying to open one from azure blob or you have valid URl.

    WebView webView = new WebView();
    var page = new ContentPage();
    webView.Source = "https://docs.google.com/viewer?url=http://yourpdfurl.pdf";
    page.Content = webView;
    Navigation.PushModalAsync(page);
    
    0 讨论(0)
  • 2020-12-12 04:40

    If I remember correctly, you can display a "remote" PDF in a webview on iOS. With Android there are some problems. I suggest to take a look to this repo OpenPDF. I use a CustomRenderer and a google tool to display the PDF. I have had some problems with some Android version... but you can take a look.(here a blog)

    Otherwise there is @AdamPedley suggestion to use PDF JS Viewer Adam Pedley The code is here.

    This is the CustomRenderer for Android

    [assembly: ExportRenderer (typeof(CustomWebView), typeof(CustomWebViewRenderer))]
    namespace DisplayPDF.Droid
    {
        public class CustomWebViewRenderer : WebViewRenderer
        {
            protected override void OnElementChanged (ElementChangedEventArgs<WebView> e)
            {
                base.OnElementChanged (e);
    
                if (e.NewElement != null) {
                    var customWebView = Element as CustomWebView;
                    Control.Settings.AllowUniversalAccessFromFileURLs = true;
                    Control.LoadUrl (string.Format ("file:///android_asset/pdfjs/web/viewer.html?file={0}", string.Format ("file:///android_asset/Content/{0}", WebUtility.UrlEncode (customWebView.Uri))));
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题