0

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))));
  • The Sharepoint links that I have seen are not actually the file itself, but Sharepoint's web wrapper. Try loading something like http://www.orimi.com/pdf-test.pdf or https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf to confirm that is the problem. – SushiHangover Nov 21 '19 at 07:36
  • Hi @SushiHangover, I have check your provided pdf link, which also not loading. Please correct me in above code, if i am did anything wrong. – Parthiban S Nov 21 '19 at 07:55
  • @SushiHangover I'm running into the same issue. Is there a way to go around Sharepoint's web wrapper? I can load .pdf links without any issue but Sharepoint's shared links to a pdf present the file within the web wrapper view and therefore it can't be loaded as usual – 12dollar Mar 10 '20 at 13:01
  • @12dollar Personally I do not know of a way around the wrapper (it is provides security, sharing and other features). You could just load the wrapper into a WebView, or an app embedded browser (ie. Chrome CustomTabs) via Essentials, etc... – SushiHangover Mar 10 '20 at 15:44
  • 1
    @SushiHangover thanks gonna try out something and I'll report back here if I've found a useable solution. – 12dollar Mar 16 '20 at 11:58

1 Answers1

0

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 .

Community
  • 1
  • 1
Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22