在Xamarin.forms中打开Pdf文件

Eng*_*eah 9 xamarin xamarin.forms

目前,我需要为应用程序编写隐私政策,并且我输入PDF格式,请问如何打开Xamarin.Forms的PDF文件?任何参考源代码?

谢谢你的分享.

Ger*_*uis 16

如果您的PDF文件托管在其他地方或本地设备上,这一切都取决于一个事实.

托管

如果是在线托管; 最简单的方法是Device.OpenUri()使用PDF文件的URI进行操作,但这会打开外部浏览器.

如果您想将其合并到您的应用程序中,您可以创建一个页面,WebView并在其中导航到PDF.这应该是非常简单的.

用a实现一个页面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>
Run Code Online (Sandbox Code Playgroud)

本地

设备上的本地没有统一的方法来执行此操作.一种方法是通过在a中显示它来实现它WebView,这与WebView上面的基本相同,但是你需要为CustomRenderer你想要使用的每个平台创建一个.您可以使用dylansturg提供的链接作为参考.

基本上,您使用自定义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>
Run Code Online (Sandbox Code Playgroud)

您的自定义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); }
    }
}
Run Code Online (Sandbox Code Playgroud)

它基本上只是一个,WebView但我们Uri为它添加了一个属性.

iOS版

现在,如果您想为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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,您将看到我们如何创建本机UIWebView并将其导航到我们的应用程序提供的文件.注意:该文件应位于内容文件中并标记为BundleResource.如果要下载文件(或策略的新版本),请确保相应地调整路径.

Android的

对于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))));
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意我们如何在此处导航到pdf.js库并为其提供查询参数以指定我们的PDF文件.同样,这是您提供应用程序的文件.此外,这仅适用于API 19及更高版本.

对于Windows,您还应该使用pdf.js库.

根据Nikolai的善意回应,似乎还有一个轻量级的pdf.js lib for mobile我自己没有使用它,但似乎更适合移动使用.

由于这个问题似乎仍然相关,我认为用更全面的博客文章来更新它是很好的.

  • 我不建议使用pdf.js _web_ viewer,因为它很重.我宁愿使用pdf.js [_mobile_ viewer](https://github.com/mozilla/pdf.js/tree/master/examples/mobile-viewer). (4认同)