Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
705 views
in Technique[技术] by (71.8m points)

xamarin.forms - How to load PDF in Xamarin Forms

I have a Xamarin Forms app where I want to open a locally stored PDF. I don't need to load them within the app, I'm fine with shelling out to the device's default document viewer for PDFs. How can I do this?

I tried sending a WebView to the PDF, but that didn't work, I just got a blank page.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've recently done this in my own project using a custom renderer. First implement an empty Xamarin forms view such as (I've included a bindable FilePath attribute):

    public class PdfViewer : View
{
    public static readonly BindableProperty FilePathProperty =
        BindableProperty.Create<DocumentViewer, string>(p => p.FilePath, null);

    public string FilePath
    {
        get
        {
            return (string)this.GetValue(FilePathProperty);
        }

        set
        {
            this.SetValue(FilePathProperty, value);
        }
    }
}

Then create an iOS Renderer that will be registered for this control. This renderer can, as it is within an iOS project, use the Quick Look Preview Controller to delegate to the built in iOS pdf viewer:

[assembly: ExportRenderer(typeof(PdfViewer), typeof(DocumentViewRenderer))]

public class DocumentViewRenderer 
        : ViewRenderer<PdfViewer, UIView>
{
    private QLPreviewController controller;

    protected override void OnElementChanged(ElementChangedEventArgs<DocumentViewer> e)
    {
        base.OnElementChanged(e);

        this.controller = new QLPreviewController();
        this.controller.DataSource = new DocumentQLPreviewControllerDataSource(e.NewElement.FilePath);

        SetNativeControl(this.controller.View);
    }

    private class DocumentQLPreviewControllerDataSource : QLPreviewControllerDataSource 
    {
        private string fileName;
        public DocumentQLPreviewControllerDataSource(string fileName)
        {
            this.fileName = fileName;
        }

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

        public override QLPreviewItem GetPreviewItem(QLPreviewController controller, int index)
        {
            var documents = NSBundle.MainBundle.BundlePath;
            var library = Path.Combine(documents, this.fileName);
            NSUrl url = NSUrl.FromFilename(library);

            return new QlItem(string.Empty, url);
        }

        private class QlItem : QLPreviewItem 
        { 
            public QlItem(string title, NSUrl uri) 
            { 
                this.ItemTitle = title; 
                this.ItemUrl = uri; 
            } 

            public override string ItemTitle { get; private set; }

            public override NSUrl ItemUrl { get; private set; }
        }
    }
}

I haven't compiled and run this as I've extracted it from my larger project but in general this should work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...