本文介绍了如何在 Xamarin webview 中加载在线 pdf 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Xamarin 表单 Webview 中加载和显示在线 pdf 文件(来自 sharepoint 链接的文件).为了加载 pdf,我使用自定义渲染器实现了 pdf 查看器.但是,没有加载文件.你能帮我解决这个问题吗?

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))));

推荐答案

如果url包含前缀https,则应在Android项目中添加如下代码.因为在 Android 9(API 级别 28)之后,默认情况下禁用明文支持.

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.

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

既然你想加载远程pdf,你只需要直接加载url.file:///android_asset/pdfjs/web/viewer.html 用于本地pdf.

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"));

我使用了 @SushiHangover 提供的 url,它在我这边工作正常.

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

这篇关于如何在 Xamarin webview 中加载在线 pdf 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 00:33