How to Display PDF in Xamarin Forms With Ease

Recently, I was researching different ways of displaying PDFs within our Xamarin Forms Apps, and I found that it all boils down to the following approaches.

The first thing we need to know is that it all depends on a few gotchas, like where is our PDF stored? Will it be locally stored on the device or remotely? Do we need to implement a viewer inside the app? Can we delegate the work to the Browser or any existing app?

Let’s Get Started!

File is stored remotely

Delegate work to Device Browser: If the PDF is stored remotely, we can simply pass the URL to the device browser and customize it. We can achieve this by leveraging Xamarin.Essentials library.

Code Snippet:

More info:
https://docs.microsoft.com/en-us/xamarin/essentials/open-browser?context=xamarin%2Fandroid&tabs=android

Upgrading from Plugins to Xamarin.Essentials - James MontemagnoXamarin.Essentials Open Browser – Xamarin The Browser class in Xamarin.Essentials enable an application to open a web link in the optimized system preferred browser or the external browser.docs.microsoft.com

File Stored in Device: delegating work to any existing app on the device

Delegate work to Device Launcher: This allows the user to select which App opens the PDF file such as Google Drive PDF Viewer. This can be achieved by leveraging Xamarin.Essentials library.

https://docs.microsoft.com/en-us/xamarin/essentials/launcher?context=xamarin%2Fandroid&tabs=android

Upgrading from Plugins to Xamarin.Essentials - James MontemagnoXamarin.Essentials Launcher – Xamarin The Launcher class in Xamarin.Essentials enable an application to open a URI by the system.docs.microsoft.com

File Stored in Device: embedded inside our app solutions

We can achieve this with the following two approaches:

Implement a Custom Viewer solution: We could implement a Custom Viewer to display the PDF directly in the app. We could also take advantage of a third party licensed component like Syncfusion, which offers you a bunch of features besides displaying just the PDF.

There is a great post made by one of our awesome community member Gerald Versluis

Custom Viewer:
https://blog.verslu.is/xamarin/xamarin-forms-xamarin/showing-pdf-files-xamarin-forms/

ImageShowing PDF files in Xamarin.Forms – Gerald Versluis This will be just a quick-and-dirty blog on showing PDF files in your Xamarin.Forms application. I got the question from an Italian friend who wanted to implement this into an app, so I thought I’d share it with the rest of you!blog.verslu.is
Note: UIWebView is now deprecated for iOS, and you must use WKWebView instead. -Virsanna

iOS Solution for Deprecated WebViewRenderer, use a WKWebViewRenderer by Vira Gryaznova – Virsanna

Syncfusion PDF Viewer:
https://www.syncfusion.com/xamarin-ui-controls/xamarin-pdf-viewer

NET, Xamarin, JavaScript, Angular UI components | SyncfusionXamarin PDF Viewer | View and review PDF files | Syncfusion Overview: The Xamarin PDF Viewer is a feature-rich and high-performance control to view and review PDF files in Xamarin.Forms applications. The bookmark, hyperlink, and table of contents support provide easy navigation within and outside PDF files. www.syncfusion.com

Ce Finito! 🎊☺️

Great Job! we are now able to display pdfs in any scenario we could encounter. I hope you have a great time trying out all of the solutions and implementing them in your future projects.

Made with ❤ by Pujolsluis

Special Thanks to Virsanna, Faiza Khan, Damian Castroviejo for their great contributions on this article. 🥳

(Visited 7,837 times, 2 visits today)

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.