How to view a PDF file using Xamarin Forms

Is it possible to use a xamarin form to view a PDF file without using a custom renderer.

+3


source to share


2 answers


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 is a little more complex and requires additional 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 great for me. The PDFItem and PDFPreviewControllerDataSource classes from the blog I can't live for, remember the name, but that's not my job. FindNavigationController might not be needed in your case, it might just be:

UISharedApplication.KeyWindow.PresentViewController

      

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

+5


source


There is now a recipe covering this: https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/controls/display-pdf/



Edit: Unfortunately, the recipe uses custom renderers. This can be useful if you cannot get it to work without the Custom Renderer.

+3


source







All Articles