How to view PDF file using Xamarin Forms

Android:

public void OpenPdf(string filePath)
{
    Android.Net.Uri uri = Android.Net.Uri.Parse("file:///" + 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();
    }
}

iOS, a bit more complex and requiries some extra classes:

public void OpenPDF(string filePath)
{
    FileInfo fi = new FileInfo(filePath);

    QLPreviewController previewController = new QLPreviewController();
    previewController.DataSource = new PDFPreviewControllerDataSource(fi.FullName, fi.Name);

    UINavigationController controller = FindNavigationController();
    if (controller != null)
        controller.PresentViewController(previewController, true, null);
}

private UINavigationController FindNavigationController()
{
    foreach (var window in UIApplication.SharedApplication.Windows)
    {
        if (window.RootViewController.NavigationController != null)
            return window.RootViewController.NavigationController;
        else
        {
            UINavigationController val = CheckSubs(window.RootViewController.ChildViewControllers);
            if (val != null)
                return val;
        }
    }

    return null;
}

private UINavigationController CheckSubs(UIViewController[] controllers)
{
    foreach (var controller in controllers)
    {
        if (controller.NavigationController != null)
            return controller.NavigationController;
        else
        {
            UINavigationController val = CheckSubs(controller.ChildViewControllers);
            if (val != null)
                return val;
        }
    }
    return null;
}

public class PDFItem : QLPreviewItem
{
    string title;
    string uri;

    public PDFItem(string title, string uri)
    {
        this.title = title;
        this.uri = uri;
    }

    public override string ItemTitle
    {
        get { return title; }
    }

    public override NSUrl ItemUrl
    {
        get { return NSUrl.FromFilename(uri); }
    }
}

public class PDFPreviewControllerDataSource : QLPreviewControllerDataSource
{
    string url = "";
    string filename = "";

    public PDFPreviewControllerDataSource(string url, string filename)
    {
        this.url = url;
        this.filename = filename;
    }

    public override QLPreviewItem GetPreviewItem(QLPreviewController controller, int index)
    {
        return new PDFItem(filename, url);
    }

    public override int PreviewItemCount(QLPreviewController controller)
    {
        return 1;
    }
}

Use a dependency service to get them and call as needed, works very well for me. The PDFItem and PDFPreviewControllerDataSource classes are from a blog that i can’t for the life of me remember the name of but they are not my work. The FindNavigationController may not be necessary in your case, it may be as simple as:

UISharedApplication.KeyWindow.PresentViewController

You can find the information here:
https://forums.xamarin.com/discussion/25746/how-to-open-pdf-in-xamarin-forms

Leave a Comment