Xamarin.forms display PDF in webview not working

后端 未结 2 1906
粉色の甜心
粉色の甜心 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: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 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))));
                }
            }
        }
    }
    

提交回复
热议问题