在Xamarin.forms中打开Pdf文件 [英] Open Pdf File in Xamarin.forms

查看:116
本文介绍了在Xamarin.forms中打开Pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我需要为应用编写隐私权政策,并以PDF格式输入, 我可以问一下如何打开Xamarin.Forms的PDF文件吗? 有参考源代码吗?

Currently, I need to write Privacy policy for apps and I put in PDF format, May I ask about how to open PDF file for Xamarin.Forms? Any references Source Code?

感谢您的分享.

推荐答案

这完全取决于您的PDF文件是托管在设备的其他位置还是本地.

It all depends a bit on the fact if your PDF file is hosted elsewhere or locally on the device.

如果它是在线托管的;最简单的方法是对PDF文件的URI进行Device.OpenUri(),但这将打开一个外部浏览器.

If it is hosted online; the easiest way would be to just do a Device.OpenUri() with the URI of the PDF file, but this would open an external browser.

如果要将其合并到应用程序中,则可以创建带有WebView的页面并导航至其中的PDF.这应该很简单.

If you want to incorporate it within your app you can create a page with a WebView and navigate to the PDF in there. This should be pretty straight-forward.

WebView填充页面,我假设您使用XAML.

Implement a page with a WebView, I'm going to assume you use 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提供的链接作为参考.

Local

Local on the device there is no unified way to do this. One way is to implement it through showing it in a WebView, which is basically the same as the WebView above, but you need to create a CustomRenderer for each platform you want to use. You can use the link provided by dylansturg as a reference.

基本上,您可以使用自定义的WebView组件来实现同一页面.

Basically you implement the same page with a custom WebView component.

<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将如下所示:

Your custom WebView will look like this:

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属性.

It's basically just a WebView but we add a Uri property to it.

现在,如果要在iOS上实现它,请在平台项目中创建一个CustomRenderer并以如下方式实现它:

Now if you want to implement it for iOS, create a CustomRenderer in your platform project and implement it somewhat like this:

[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.如果要下载文件(或策略的新版本),请确保相应地调整路径.

Here you see how we create a native UIWebView and navigate it a file which is provided with our app. Note: the file should be in the Content file and marked as a BundleResource. If you are to download the file (or new versions of the policy) make sure you adjust the path accordingly.

对于Android来说,还有更多工作要做,您必须下载 pdf.js 库将其添加到资产"下的项目中.确保检查文件是否也已添加到存储库中.就我而言,.js文件默认位于我的.gitignore文件中.

For Android it's a bit more work, you have to download the pdf.js library add it to you project under Assets. Make sure to check if the files are added to your repository as well. In my case the .js files where in my .gitignore file by default.

现在创建像这样的自定义渲染器:

Now create a custom renderer like this:

[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或更高版本使用.

Notice how we navigate to the pdf.js library here and provide it with a query parameter to specify our PDF file. Again, this is when you provide the file with the app. Also this is only available from API 19 and up.

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

For Windows you should also use the pdf.js library.

根据尼古拉的善意回应,似乎还有一个轻量级的 pdf.js用于移动的库我自己还没有使用它,但似乎可用于移动用途.

As per Nikolai's kind response it seems there is also a lightweight pdf.js lib for mobile I didn't us it myself yet, but it seems better to use for mobile use.

因为这个问题似乎仍然很重要,所以我认为最好使用更全面的

Since this question still seems relevant I thought it would be nice to update it with a more thorough blog post I did on this.

这篇关于在Xamarin.forms中打开Pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆