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. using Xamarin.Forms;
    6. namespace DownloadPDF
    7. {
    8. public partial class App : Application
    9. {
    10. public App ()
    11. {
    12. InitializeComponent();
    13. MainPage = new NavigationPage(new MainPage());
    14. }
    15. protected override void OnStart ()
    16. {
    17. // Handle when your app starts
    18. }
    19. protected override void OnSleep ()
    20. {
    21. // Handle when your app sleeps
    22. }
    23. protected override void OnResume ()
    24. {
    25. // Handle when your app resumes
    26. }
    27. }
    28. }
  • 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. <StackLayout>
    8. <WebView x:Name="Browser"
    9. Source ="http://www.pdfpdf.com/samples.html"
    10. Navigating="webOnNavigating"
    11. HorizontalOptions="FillAndExpand"
    12. VerticalOptions="FillAndExpand"/>
    13. </StackLayout>
    14. </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. namespace DownloadPDF
    8. {
    9. public partial class MainPage : ContentPage
    10. {
    11. public MainPage()
    12. {
    13. InitializeComponent();
    14. }
    15. protected void webOnNavigating(object sender, WebNavigatingEventArgs e)
    16. {
    17. if (e.Url.Contains(".pdf"))
    18. {
    19. // Retrieving the URL
    20. var pdfUrl = new Uri(e.Url);
    21. // Open PDF URL with device browser to download
    22. Device.OpenUri(pdfUrl);
    23. // Cancel the navigation on click actions
    24. // (retains in the same page.)
    25. e.Cancel = true;
    26. }
    27. }
    28. }
    29. }
  • 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.