在 Xamarin Web View 中显示 PDF



如何在不使用外部渲染引擎的情况下下载PDF文件并在WebView中显示它?

WebView webview = FindViewById<WebView>(Resource.Id.webView1);
webview.Settings.JavaScriptEnabled = true;
webview.Settings.AllowFileAccess = true;
webview.LoadUrl("http://www.justadomain.com/document.pdf");

还有许多提供PDF SDK的Xamarin组件,但不幸的是成本很高。对于其他免费的替代方案,您可以随时查看此博客文章,其中说明了在应用程序中显示PDF文件时的选项。

最简单,也许是最干净的解决方案是让第3方应用程序显示您的PDF文件。以下是从上面的博客文章中获取的java代码:

using Uri = Android.Net.Uri;
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(Uri.Parse("file:///" + PathToFile(filename)), "application/pdf");
intent.SetFlags(ActivityFlags.ClearTop);
StartActivity(intent);
return null;

否则,您也可以尝试查找像这样的AndroidPDF库,并通过java绑定将其导入Xamarin项目。

希望这有帮助。

试试这个:

webview.LoadUrl("https://docs.google.com/viewer?url="+"http://www.justadomain.com/document.pdf");

Web 引擎无法做到这一点!

如果你想显示一个PDF,你需要一种方法来将其转换为某种HTML。有许多关于此的工具,例如Google Drive或Mozillas PDF.js

最新更新