I recently did this in my own project using my own renderer. First, create an empty view of Xamarin forms, for example (I have included the associated 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, like in the framework of the iOS project, use the Quick Preview Preview Controller to transfer pdf to the iOS browser:
[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 did not compile or run this, as I extracted it from my larger project, but overall this should work.
Cargowire
source share