在Xamarin.Forms UWP中显示来自互联网的pdf文件 [英] Displaying pdf files from internet in Xamarin.Forms UWP

查看:131
本文介绍了在Xamarin.Forms UWP中显示来自互联网的pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Xamarin.Forms应用程序,仅支持UWP.我需要能够从网上加载pdf文件并在我的应用程序中显示内容.我找不到能与UWP一起使用并处理不属于项目的pdf文件的解决方案.

I have a Xamarin.Forms application supporting only UWP. I need to be able to load pdf files from the web and display the content in my application. I cannot find a solution that would work with UWP and handle pdf files that are not part of a project.

推荐答案

根据您的要求,您可以参考 pdf.js 主机Web应用程序.

For your requirement, you could refer this code sample that custom a WebView and load pdf file with pdf.js host web application.

[assembly: ExportRenderer(typeof(CustomWebView), typeof(CustomWebViewRenderer))]
namespace DisplayPDF.WinPhone81
{
    public class CustomWebViewRenderer : WebViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                var customWebView = Element as CustomWebView;
                Control.Source = new Uri(string.Format("ms-appx-web:///Assets/pdfjs/web/viewer.html?file={0}", string.Format ("ms-appx-web:///Assets/Content/{0}", WebUtility.UrlEncode(customWebView.Uri))));
            }
        }
    }
}


更新

以这种方式工作.但是pdf应该是项目的一部分.这是否意味着没有办法显示随机文件,例如是从网上下载的?

This way it works. But the pdf should be a part of the project. Does it mean that there is no way to display random files, e.g. downloaded from the web?

我回想起过去几个月来我回答的案件.为了显示随机文件,您可以将pdf文件转换为Base64String,然后通过在viewer.js中调用openPdfAsBase64 JS函数来打开它.有关详细信息,请参考此案例回复.

I recollect the case that I answered in the last few months. For displaying random files, you could convert pdf file into Base64String then opening it by calling the openPdfAsBase64 JS function in the viewer.js. For detail please refer this case reply.

为了更好地理解,我创建了一个代码示例,您可以参考它.使用它之前,您需要hello.pdf文件放置在LocalState 文件夹中.因为,默认的文件加载路径是LocalState文件夹,但是在开始时它是空的.

For better understand, I have create a code sample that you could refer. before using it you need place hello.pdf file in the LocalState folder. Because, the default file load path is LocalState folder, but it is empty at fist start.

PDFViewRenderer.cs

public class PDFViewRenderer :WebViewRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
    {
        base.OnElementChanged(e);
        if (e.NewElement != null)
        {
            Control.Source = new Uri("ms-appx-web:///Assets/pdfjs/web/viewer.html");
            Control.LoadCompleted += Control_LoadCompleted;
        }
    }
    private async void Control_LoadCompleted(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e)
    {
        PDFView pdfView = Element as PDFView;
        if (string.IsNullOrEmpty(pdfView?.FileName)) return;
        try
        {
            var Base64Data = await OpenAndConvert(pdfView?.FileName);
            var obj = await Control.InvokeScriptAsync("openPdfAsBase64", new[] { Base64Data });
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    private async Task<string> OpenAndConvert(string FileName)
    {
        var folder = ApplicationData.Current.LocalFolder;
        var file = await folder.GetFileAsync(FileName);
        var filebuffer = await file.OpenAsync(FileAccessMode.Read);
        var reader = new DataReader(filebuffer.GetInputStreamAt(0));
        var bytes = new byte[filebuffer.Size];
        await reader.LoadAsync((uint)filebuffer.Size);
        reader.ReadBytes(bytes);
        return Convert.ToBase64String(bytes);
    }
}

PDFView.cs

public class PDFView : WebView
{
    public PDFView()
    {

    }
    public static readonly BindableProperty FileNameProperty = BindableProperty.Create(
    propertyName: "FileName",
    returnType: typeof(string),
    declaringType: typeof(PDFView),
    defaultValue: default(string));

    public string FileName
    {
        get { return (string)GetValue(FileNameProperty); }
        set { SetValue(FileNameProperty, value); }
    }
}

用法

<Grid>
    <local:PDFView FileName="hello.pdf"/>
</Grid>

请注意,您需要使用此 viewer.js (已添加openPdfAsBase64方法).

Please note you need use this viewer.js that was added openPdfAsBase64 method.

这篇关于在Xamarin.Forms UWP中显示来自互联网的pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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