Tuesday, 22 May 2018

Download PDF into device on tapping/clicking link button in Webview using Xamarin Forms

Hi Everyone,

In this post I will gonna be explaining about how to download PDF file into device on tapping/clicking the link button in webview Xamarin Forms.

Please follow the below steps to accomplish above task.

  • Create a PCL/NET standard project. In my case I named it as “DownloadPDF.
  • Paste the following code in App.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Xamarin.Forms;

namespace DownloadPDF
{
       public partial class App : Application
       {
              public App ()
              {
                     InitializeComponent();

                     MainPage = new NavigationPage(new MainPage());
              }

              protected override void OnStart ()
              {
                     // Handle when your app starts
              }

              protected override void OnSleep ()
              {
                     // Handle when your app sleeps
              }

              protected override void OnResume ()
              {
                     // Handle when your app resumes
              }
       }
}

·         Paste the following code MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:DownloadPDF"
      Title="Download PDF"
             x:Class="DownloadPDF.MainPage">

    <StackLayout>
        <WebView x:Name="Browser"
                 Source ="http://www.pdfpdf.com/samples.html"
                 Navigating="webOnNavigating"
                 HorizontalOptions="FillAndExpand"
                 VerticalOptions="FillAndExpand"/>
    </StackLayout>

</ContentPage>

              Here, I am giving http://www.pdfpdf.com/samples.html as source. Actually it contains some                - sample PDF files. I will try to download those PDF file(s).
  •  Paste the following code MainPage.xaml.cs

            using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace DownloadPDF
{
       public partial class MainPage : ContentPage
       {
              public MainPage()
              {
                     InitializeComponent();
              }

protected void webOnNavigating(object sender, WebNavigatingEventArgs e)
              {
                     if (e.Url.Contains(".pdf"))
                     {
                           // Retrieving the URL
                           var pdfUrl = new Uri(e.Url);

                           // Open PDF URL with device browser to download
                           Device.OpenUri(pdfUrl);

                           // Cancel the navigation on click actions
              // (retains in the same page.)
                           e.Cancel = true;
                     }
              }
       }
}

After then, set Android or iOS project as a startup project, run the application on the respective platforms.

To view full source code, please click here

Output:






Please raise your doubts in the comments section, will back to you with answers.


Thanks for reading the article.

No comments:

Post a Comment

Intoduction to Flutter

Hi All, I hope every one is doing good In this article, we will know about the introduction of Flutter.