如何使用 Xamarin Forms 查看 PDF 文件 [英] How to view PDF file using Xamarin Forms

查看:30
本文介绍了如何使用 Xamarin Forms 查看 PDF 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以使用 xamarin 表单在不使用自定义渲染器的情况下查看 PDF 文件.

Is there any way I can use xamarin form to view a PDF file without using custom renderer.

推荐答案

Android:

public void OpenPdf(string filePath)
{
    Android.Net.Uri uri = Android.Net.Uri.Parse("file:///" + filePath);
    Intent intent = new Intent(Intent.ActionView);
    intent.SetDataAndType(uri, "application/pdf");
    intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);

    try
    {
        Xamarin.Forms.Forms.Context.StartActivity(intent);
    }
    catch (Exception)
    {
        Toast.MakeText(Xamarin.Forms.Forms.Context, "No Application Available to View PDF", ToastLength.Short).Show();
    }
}

iOS,有点复杂,需要一些额外的类:

iOS, a bit more complex and requiries some extra classes:

public void OpenPDF(string filePath)
{
    FileInfo fi = new FileInfo(filePath);

    QLPreviewController previewController = new QLPreviewController();
    previewController.DataSource = new PDFPreviewControllerDataSource(fi.FullName, fi.Name);

    UINavigationController controller = FindNavigationController();
    if (controller != null)
        controller.PresentViewController(previewController, true, null);
}

private UINavigationController FindNavigationController()
{
    foreach (var window in UIApplication.SharedApplication.Windows)
    {
        if (window.RootViewController.NavigationController != null)
            return window.RootViewController.NavigationController;
        else
        {
            UINavigationController val = CheckSubs(window.RootViewController.ChildViewControllers);
            if (val != null)
                return val;
        }
    }

    return null;
}

private UINavigationController CheckSubs(UIViewController[] controllers)
{
    foreach (var controller in controllers)
    {
        if (controller.NavigationController != null)
            return controller.NavigationController;
        else
        {
            UINavigationController val = CheckSubs(controller.ChildViewControllers);
            if (val != null)
                return val;
        }
    }
    return null;
}

public class PDFItem : QLPreviewItem
{
    string title;
    string uri;

    public PDFItem(string title, string uri)
    {
        this.title = title;
        this.uri = uri;
    }

    public override string ItemTitle
    {
        get { return title; }
    }

    public override NSUrl ItemUrl
    {
        get { return NSUrl.FromFilename(uri); }
    }
}

public class PDFPreviewControllerDataSource : QLPreviewControllerDataSource
{
    string url = "";
    string filename = "";

    public PDFPreviewControllerDataSource(string url, string filename)
    {
        this.url = url;
        this.filename = filename;
    }

    public override QLPreviewItem GetPreviewItem(QLPreviewController controller, int index)
    {
        return new PDFItem(filename, url);
    }

    public override int PreviewItemCount(QLPreviewController controller)
    {
        return 1;
    }
}

使用依赖服务来获取它们并根据需要调用,对我来说效果很好.PDFItem 和 PDFPreviewControllerDataSource 类来自一个博客,我一辈子都记不起它的名字,但它们不是我的作品.FindNavigationController 在您的情况下可能不是必需的,它可能很简单:

Use a dependency service to get them and call as needed, works very well for me. The PDFItem and PDFPreviewControllerDataSource classes are from a blog that i can't for the life of me remember the name of but they are not my work. The FindNavigationController may not be necessary in your case, it may be as simple as:

UISharedApplication.KeyWindow.PresentViewController

您可以在此处找到信息:https://forums.xamarin.com/discussion/25746/how-to-open-pdf-in-xamarin-forms

You can find the information here: https://forums.xamarin.com/discussion/25746/how-to-open-pdf-in-xamarin-forms

这篇关于如何使用 Xamarin Forms 查看 PDF 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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