简体   繁体   English

在 Xamarin WebView 中显示 PDF

[英]Display a PDF in a Xamarin WebView

How can I download a PDF file and display it within a WebView without using an external rendering engine?如何在不使用外部渲染引擎的情况下下载 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");

试试这个:

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

There are also many Xamarin components that offer PDF SDKs, but are unfortunately costly.还有许多 Xamarin 组件提供 PDF SDK,但不幸的是价格昂贵。 For other free alternatives, you can always check this blog post which states the options you have when displaying PDF files in your application.对于其他免费替代方案,您可以随时查看此博客文章,其中说明了在应用程序中显示 PDF 文件时的选项。

The easiest and maybe the cleanest solution is to let a 3rd-party application display your PDF file.最简单也可能是最干净的解决方案是让第 3 方应用程序显示您的 PDF 文件。 Here is the java code taken from the blog post above:这是从上面的博客文章中提取的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;

Otherwise, you can also try to find a PDF library for Android like this one and, via java bindings, import it into your Xamarin project.否则,您也可以尝试为 Android 找到一个像这样的 PDF 库,并通过 java 绑定将其导入您的 Xamarin 项目。

Hope this helps.希望这会有所帮助。

The web engine is not capable of doing this! Web 引擎无法做到这一点!

If you want to display a PDF anyway you need a way to convert it to some kind of HTML.如果您无论如何都想显示 PDF,则需要一种将其转换为某种 HTML 的方法。 There are many tools regarding this, for example Google Drive or Mozillas PDF.js有很多关于此的工具,例如Google DriveMozillas PDF.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM