Implementing a Cross-Platform PDF Viewer in Xamarin.Forms and Android

Posted by Matheus Guimaraes |26 Sep 15 |

In this post I’m going to show you how to implement a cross-platform PDF viewer using Xamarin.Forms. We are going to focus on  Android in this tutorial.

The approach we are going to take is to create a Dependency Service implementation on the Android project which triggers a choice for a external PDF viewing app on the user’s phone.

If you are wondering why not just use an embedded WebView to display PDFs, the short answer is that while it would work on IOS this wouldn’t work on any Android versions prior to Lollipop. Also, from a technical strategy and commercial point it may be more beneficial to allow the users freedom of choice. For more on that, you can read my previous blog post WebView Or External Web Picker? Or, When To Give Users Freedom .

Step 1 – Create the Dependency Service contract

On the PCL or SAP project Xamarin.Forms project, create the following interface:

public interface IPdfViewer
{
  void View(string filePath);
}

Step 2 – Create the Android implementation

On your Android project, create the following class:

public class PdfViewer : IPdfViewer
    {
        public void View(string filePath)
        {
            //copy to global
           
            var localFileStore = new LocalFileStore();
            localFileStore.CopyToGlobalStorage(filePath);
 
            Android.Net.Uri uri = Android.Net.Uri.Parse("file:///" + localFileStore.GetGlobalPath(filePath));
            Intent intent = new Intent(Intent.ActionView);
            intent.SetDataAndType(uri, "application/pdf");
            intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
 
            try
            {
                Xamarin.Forms.Forms.Context.StartActivity(intent);
            }
            catch (Exception)
            {
                Toast.MakeText(Xamarin.Forms.Forms.Context, "No Application Available to View PDF", ToastLength.Short).Show();
            }
        }
    }