目前,我需要为应用程序编写隐私政策,我将其放入PDF格式, 请问如何打开Xamarin.Forms的PDF文件? 任何参考源代码?
感谢您的分享。
答案 0 :(得分:15)
如果您的PDF文件托管在其他地方或本地设备上,则完全取决于这一事实。
如果是在线托管;最简单的方法是使用PDF文件的URI进行Device.OpenUri()
,但这会打开外部浏览器。
如果您想将其合并到您的应用中,可以创建一个带有WebView
的页面,然后导航到那里的PDF。这应该是非常简单的。
使用WebView
实现一个页面,我将假设您使用XAML。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="DisplayPDF.WebViewPage"
Padding="0,20,0,0">
<ContentPage.Content>
<WebView Source="http://www.yoursite.com/your.pdf" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
</ContentPage.Content>
</ContentPage>
设备上的本地没有统一的方法来执行此操作。一种方法是通过在WebView
中显示它来实现它,这与上面的WebView
基本相同,但您需要为要使用的每个平台创建CustomRenderer
。您可以使用dylansturg提供的the link作为参考。
基本上,您使用自定义WebView
组件实现相同的页面。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:DisplayPDF;assembly=DisplayPDF"
x:Class="DisplayPDF.WebViewPage"
Padding="0,20,0,0">
<ContentPage.Content>
<local:CustomWebView Uri="your.pdf" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
</ContentPage.Content>
</ContentPage>
您的自定义WebView
将如下所示:
public class CustomWebView : WebView
{
public static readonly BindableProperty UriProperty = BindableProperty.Create (propertyName:"Uri",
returnType:typeof(string),
declaringType:typeof(CustomWebView),
defaultValue:default(string));
public string Uri {
get { return (string)GetValue (UriProperty); }
set { SetValue (UriProperty, value); }
}
}
它基本上只是WebView
,但我们会为其添加Uri
属性。
现在,如果您想为iOS实现它,请在您的平台项目中创建一个CustomRenderer
并按照以下方式实现它:
[assembly: ExportRenderer (typeof(CustomWebView), typeof(CustomWebViewRenderer))]
namespace DisplayPDF.iOS
{
public class CustomWebViewRenderer : ViewRenderer<CustomWebView, UIWebView>
{
protected override void OnElementChanged (ElementChangedEventArgs<CustomWebView> e)
{
base.OnElementChanged (e);
if (Control == null) {
SetNativeControl (new UIWebView ());
}
if (e.OldElement != null) {
// Cleanup
}
if (e.NewElement != null) {
var customWebView = Element as CustomWebView;
string fileName = Path.Combine (NSBundle.MainBundle.BundlePath, string.Format ("Content/{0}", WebUtility.UrlEncode (customWebView.Uri)));
Control.LoadRequest (new NSUrlRequest (new NSUrl (fileName, false)));
Control.ScalesPageToFit = true;
}
}
}
}
在这里,您可以看到我们如何创建原生UIWebView
并将其导航到我们的应用提供的文件中。注意:该文件应位于内容文件中,并标记为BundleResource
。如果要下载文件(或新版本的策略),请确保相应地调整路径。
对于Android,它需要做更多的工作,你必须下载pdf.js库,将它添加到Assets下的项目中。确保检查文件是否也已添加到存储库中。在我的例子中,默认情况下我的.gitignore
文件中的.js文件。
现在创建一个这样的自定义渲染器:
[assembly: ExportRenderer (typeof(CustomWebView), typeof(CustomWebViewRenderer))]
namespace DisplayPDF.Droid
{
public class CustomWebViewRenderer : WebViewRenderer
{
protected override void OnElementChanged (ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged (e);
if (e.NewElement != null) {
var customWebView = Element as CustomWebView;
Control.Settings.AllowUniversalAccessFromFileURLs = true;
Control.LoadUrl (string.Format ("file:///android_asset/pdfjs/web/viewer.html?file={0}", string.Format ("file:///android_asset/Content/{0}", WebUtility.UrlEncode (customWebView.Uri))));
}
}
}
}
请注意我们如何在此处导航到pdf.js库并为其提供查询参数以指定我们的PDF文件。同样,这是您提供应用程序的文件。此外,仅适用于API 19及更高版本。
对于Windows,您还应该使用pdf.js库。
根据尼古拉的善意反应,似乎还有一个轻量级的pdf.js lib for mobile我自己也没有,但似乎更适合移动使用。
由于这个问题似乎仍然相关,我认为用更彻底的blog post更新它会更好。
答案 1 :(得分:1)
托管的PDF文件未在Webview
中打开。因为iOS Webview可以打开 PDF ,而Android Webview
不能不能打开PDF文件。因此,如果PDF网址为Public,那么您可以在不使用任何库的情况下针对Android使用以下代码,并且不需要平台特定的代码。
var pdfUrl = "http://yourdomain.com/pdffile.pdf";
var googleUrl = "http://drive.google.com/viewerng/viewer?embedded=true&url=";
if(Device.RuntimePlatform == Device.iOS)
{
webView.Source = pdfUrl;
}
else if(Device.RuntimePlatform == Device.Android)
{
webView.Source = new UrlWebViewSource() { Url = googleUrl + pdfUrl };
}
答案 2 :(得分:0)
最近我遇到了类似的问题,写了一篇博客,并向github添加了示例代码。我使用PDfJs在自定义webview中显示PDF。
链接: