How to load the online pdf files in Xamarin webview?

佐手、 提交于 2020-01-25 07:05:07

问题


I have trying to load and display the online pdf file (file from sharepoint link) in Xamarin forms Webview. To load the pdf, i have implemented the pdf viewer using custom renderer. But, there files are not loaded. Can you please help me out on this ?

Following code are used to load the file.

   var customWebView = Element as AuthWebView;
                Control.Settings.AllowUniversalAccessFromFileURLs = true;
                Control.Settings.PluginsEnabled = true;
                Control.Settings.JavaScriptEnabled = true;
                //  Control.LoadUrl("https://drive.google.com/viewerng/viewer?embedded=true&url=" + customWebView.Uri); tired this also but no luck
                Control.LoadUrl(string.Format("file:///android_asset/pdfjs/web/viewer.html?file={0}", string.Format("file:///android_asset/Content/{0}", WebUtility.UrlEncode(customWebView.Uri))));

回答1:


If the url contains the prefix https , you should add the following code in Android project . Because After Android 9 (API level 28), cleartext support is disabled by default.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        ...
        android:usesCleartextTraffic="true"
        ...>
        ...
    </application>
</manifest>

And since you want to load remote pdf , you just need to load url directly .file:///android_asset/pdfjs/web/viewer.html is for local pdf .

Control.LoadUrl(string.Format("https://drive.google.com/viewerng/viewer?url={0}", "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"));

I used the url which provided by @SushiHangover and it works fine on my side .



来源:https://stackoverflow.com/questions/58969480/how-to-load-the-online-pdf-files-in-xamarin-webview

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