Download, save and display Pdf file

MohammedAzarudeenMohammedAzarudeen USMember
edited October 2015 in Xamarin.Forms

I have the following requirements in my xamarin.forms app

  1. I need to download a pdf file from the url on button click
  2. Need to save them locally in defined folder
  3. Then i need to display with installed pdf viewer.

Any help on this? @FredyWenger @MitchMilam

Answers

  • @ApurvaGoyal @IanVink any help on this?

  • @IanVink @ApurvaGoyal I am able to download and save the pdf file locally, but cannot open the file by means of intent once the file is downloaded.

            private void SavePdf(string filename, string link)
            {
                WebClient client = new WebClient();
                string documentsPath =Path.Combine((Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads)).Path,filename);
                client.DownloadFile(link, documentsPath);
                ViewPDF(documentsPath, filename);         
    
            }
    
            private Task ViewPDF(string path, string filename)
            {
                var location = Path.Combine((Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads)).Path, filename);
                var bytes = File.ReadAllBytes(location);
    
                var externalPath = global::Android.OS.Environment.ExternalStorageDirectory.Path + "/Succintly.pdf";
                File.WriteAllBytes(externalPath, bytes);
    
                Uri pdfPath = Uri.Parse(externalPath);
                Intent intent = new Intent(Intent.ActionView);
                intent.SetDataAndType(pdfPath, "application/pdf");
                intent.SetFlags(ActivityFlags.NewTask);
                Forms.Context.StartActivity(intent);
                return Task.FromResult(true);
            }  
    
  • You can use this code for download the pdf file or any other file and open with default app.

    Code for IOS:

    public void DownloadPdfFile(byte[] bytes,string Name)
    {
    var directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    string filePath = Path.Combine(directory.ToString(), Name);
    File.WriteAllBytes(filePath, bytes);

            QLPreviewItemFileSystem prevItem = new QLPreviewItemFileSystem(Name, filePath);
            QLPreviewController previewController = new QLPreviewController();
            previewController.DataSource = new PreviewControllerDS(prevItem);
            UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(previewController, true, null);
        }
    
    public class PreviewControllerDS : QLPreviewControllerDataSource
    {
        private QLPreviewItem _item;
    
        public PreviewControllerDS(QLPreviewItem item)
        {
            _item = item;
        }
    
        public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index)
        {
            return _item;
        }
    
        public override nint PreviewItemCount(QLPreviewController controller)
        {
            return 1;
        }
    }
    
    public class QLPreviewItemFileSystem : QLPreviewItem
    {
        string _fileName, _filePath;
    
        public QLPreviewItemFileSystem(string fileName, string filePath)
        {
            _fileName = fileName;
            _filePath = filePath;
        }
    
        public override string ItemTitle
        {
            get
            {
                return _fileName;
            }
        }
        public override NSUrl ItemUrl
        {
            get
            {
                return NSUrl.FromFilename(_filePath);
            }
        }
    }
    

    Code For Android App:

    public void DownloadPdfFile(byte[] bytes, string Name)
    {
    // Write pdf File
    var directory = global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
    directory = Path.Combine(directory, Android.OS.Environment.DirectoryDownloads);
    string filePath = Path.Combine(directory.ToString(), Name);
    File.WriteAllBytes(filePath, bytes);

            //Open the Pdf file with Defualt app.
            Android.Net.Uri pdfPath = Android.Net.Uri.FromFile(new Java.IO.File(filePath));
            Intent intent = new Intent(Intent.ActionView);
            intent.SetDataAndType(pdfPath, "application/pdf");
            intent.SetFlags(ActivityFlags.NewTask);
            Forms.Context.StartActivity(intent);
        }
    
  • SamirgcSamirgc USMember ✭✭

    Your whole solution goes like this ..Please follow along to get the solution for xamarin forms :smile:

  • theramblertherambler Member ✭✭

    is there anyway of using this code but applying it for any file type that is downloaded, so it can try and open any file format?

    @Dharmandarkumar said:
    You can use this code for download the pdf file or any other file and open with default app.

    Code for IOS:

    public void DownloadPdfFile(byte[] bytes,string Name)
    {
    var directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    string filePath = Path.Combine(directory.ToString(), Name);
    File.WriteAllBytes(filePath, bytes);

            QLPreviewItemFileSystem prevItem = new QLPreviewItemFileSystem(Name, filePath);
            QLPreviewController previewController = new QLPreviewController();
            previewController.DataSource = new PreviewControllerDS(prevItem);
            UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(previewController, true, null);
        }
    
    public class PreviewControllerDS : QLPreviewControllerDataSource
    {
        private QLPreviewItem _item;
    
        public PreviewControllerDS(QLPreviewItem item)
        {
            _item = item;
        }
    
        public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index)
        {
            return _item;
        }
    
        public override nint PreviewItemCount(QLPreviewController controller)
        {
            return 1;
        }
    }
    
    public class QLPreviewItemFileSystem : QLPreviewItem
    {
        string _fileName, _filePath;
    
        public QLPreviewItemFileSystem(string fileName, string filePath)
        {
            _fileName = fileName;
            _filePath = filePath;
        }
    
        public override string ItemTitle
        {
            get
            {
                return _fileName;
            }
        }
        public override NSUrl ItemUrl
        {
            get
            {
                return NSUrl.FromFilename(_filePath);
            }
        }
    }
    

    Code For Android App:

    public void DownloadPdfFile(byte[] bytes, string Name)
    {
    // Write pdf File
    var directory = global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
    directory = Path.Combine(directory, Android.OS.Environment.DirectoryDownloads);
    string filePath = Path.Combine(directory.ToString(), Name);
    File.WriteAllBytes(filePath, bytes);

            //Open the Pdf file with Defualt app.
            Android.Net.Uri pdfPath = Android.Net.Uri.FromFile(new Java.IO.File(filePath));
            Intent intent = new Intent(Intent.ActionView);
            intent.SetDataAndType(pdfPath, "application/pdf");
            intent.SetFlags(ActivityFlags.NewTask);
            Forms.Context.StartActivity(intent);
        }
    
  • ThamaraiThamarai Member ✭✭

    In my case, I need to view the pdf file from an url, which is actually from API (file in server) on clicking button in Xamarin.Android. How can I achieve this?

  • Mayank.0569Mayank.0569 USMember

    @Thamarai I think you want to Just show PDF online, and not to download the PDF, in this case you can use Webview

  • ThamaraiThamarai Member ✭✭

    How can I achieve this in WebView under Xamarin.Android application?

Sign In or Register to comment.