How To Add "Download PDF" Functionality In Xamarin.Forms Applications

Introduction

This article demonstrates how to download PDF files into devices by tapping/clicking the link button in webview Xamarin Forms.

Prerequisites

  • Windows or Mac machine.
  • Based on the operating system, the respective Visual Studio has to be installed in it.
  • Required to have Android and iOS devices.

How can we achieve this functionality?

We can achieve this functionality by the following steps.

  • We have to handle “Navigating” event of webview, using this we can get the URL of the file on which user tapped in webview.
  • To download the PDF file, we have to pass the PDF file URL (which we got in the above step) to OpenURL() method.

Explanation

  • In the Start screen, launch Visual Studio. This opens the start page of Visual Studio like below.

    Xamarin
  • Create a new project using Visual Studio as shown below.

    Xamarin

  • If you select “Project” in the above context menu, the following window will open.

    Xamarin

In the above image,

  1. Select “Cross-Platform” option from the left lane.
  2. Choose “Mobile App (Xamarin.Forms)” option for creating Xamarin Forms application.
  3. Choose the location on disk where you want to store your application on hard disk.
  4. Give some name to your project which is relevant to your task. In my case, I’m giving it as “DownloadPDF”.
  5. After clicking “OK” button, the project will be created.
  • Paste the following code in xaml.cs file within PCL/NET standard project.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5.   
    6. using Xamarin.Forms;  
    7.   
    8. namespace DownloadPDF  
    9. {  
    10.     public partial class App : Application  
    11.     {  
    12.         public App ()  
    13.         {  
    14.             InitializeComponent();  
    15.   
    16.             MainPage = new NavigationPage(new MainPage());  
    17.         }  
    18.   
    19.         protected override void OnStart ()  
    20.         {  
    21.             // Handle when your app starts  
    22.         }  
    23.   
    24.         protected override void OnSleep ()  
    25.         {  
    26.             // Handle when your app sleeps  
    27.         }  
    28.   
    29.         protected override void OnResume ()  
    30.         {  
    31.             // Handle when your app resumes  
    32.         }  
    33.     }  
    34. }  
  • Paste the following code xaml within PCL/NET standard project .
    1. <?xml version="1.0" encoding="utf-8" ?>  
    2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
    3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
    4.              xmlns:local="clr-namespace:DownloadPDF"  
    5.       Title="Download PDF"  
    6.              x:Class="DownloadPDF.MainPage">  
    7.   
    8.     <StackLayout>  
    9.         <WebView x:Name="Browser"   
    10.                  Source ="http://www.pdfpdf.com/samples.html"  
    11.                  Navigating="webOnNavigating"  
    12.                  HorizontalOptions="FillAndExpand"  
    13.                  VerticalOptions="FillAndExpand"/>  
    14.     </StackLayout>  
    15.   
    16. </ContentPage>  

Here, I am giving http://www.pdfpdf.com/samples.html as a source. Actually, it contains some sample PDF files. I will try to download that PDF file (s).

  • Paste the following code xaml.cs which is the code behind to the Xaml page of above-pasted code.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Threading.Tasks;  
    6. using Xamarin.Forms;  
    7.   
    8. namespace DownloadPDF  
    9. {  
    10.     public partial class MainPage : ContentPage  
    11.     {  
    12.         public MainPage()  
    13.         {  
    14.             InitializeComponent();  
    15.         }  
    16.   
    17. protected void webOnNavigating(object sender, WebNavigatingEventArgs e)  
    18.             {  
    19.                     if (e.Url.Contains(".pdf"))  
    20.                     {  
    21.                         // Retrieving the URL  
    22.                         var pdfUrl = new Uri(e.Url);  
    23.   
    24.                         // Open PDF URL with device browser to download  
    25.                         Device.OpenUri(pdfUrl);  
    26.   
    27.                         // Cancel the navigation on click actions   
    28.         // (retains in the same page.)  
    29.                         e.Cancel = true;  
    30.                     }  
    31.             }  
    32.         }  
    33. }  
  • After that, set either Android or iOS project as a start up project, run the application in the respective platform and the output will be displayed like below.

Output

Xamarin

Xamarin

Xamarin

 

To view full source code, please click here.

Thanks for reading the article.


Similar Articles