How to Download PDF in Xamarin Forms

I have a Xamarin Forms application where I want to open a locally saved PDF file. I do not need to download them to the application, I am fine with shelling in the default document viewer for PDF files. How can i do this?

I tried to send WebView to PDF, but it did not work, I just got a blank page.

+8
xamarin xamarin.forms
source share
3 answers

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.

+8
source share

I needed to do something and solve it using DependencyService. You can use it to open pdf depending on each platform.

I will show you an example of how to solve it on Android:

IPdfCreator.cs

 public interface IPdfCreator { void ShowPdfFile(); } 

MainPage.cs:

 private void Button_OnClicked(object sender, EventArgs e) { DependencyService.Get<IPdfCreator>().ShowPdfFile(); } 

PdfCreatorAndroid.cs

 [assembly: Dependency(typeof(PdfCreatorAndroid))] namespace Example.Droid.DependecyServices { public class PdfCreatorAndroid : IPdfCreator { public void ShowPdfFile() { var fileLocation = "/sdcard/Template.pdf"; var file = new File(fileLocation); if (!file.Exists()) return; var intent = DisplayPdf(file); Forms.Context.StartActivity(intent); } public Intent DisplayPdf(File file) { var intent = new Intent(Intent.ActionView); var filepath = Uri.FromFile(file); intent.SetDataAndType(filepath, "application/pdf"); intent.SetFlags(ActivityFlags.ClearTop); return intent; } } } 

Result: http://i.stack.imgur.com/vrwzt.png

+6
source share

There is a good project here that uses the MuPDF Library in xamarin. I tested it and it works correctly.

With MuPDF, you can zoom out, zoom in, or even write a note in PDF files.

+1
source share

All Articles