Create a PDF control for Xamarin Forms

Create a PDF control for Xamarin Forms

It could be useful to view a PDF file directly within your App. Xamarin Forms does not provide a specific control to view PDF files, but you can simply use a WebView to solve the problem.

Our PDFView control

Let's start by defining a class that extends the WebView object and define the URI property in order to pass the path of our PDF file:

iOS

The UIWebView control allows natively to open different types of files, including PDFs. It will therefore be enough to create a Renderer that directly loads the indicated file:

Android e UWP

On these two platforms, the WebView controls do not allow you to view PDF files. However, we can use a javascript tool to render our PDF via the web. We will then use the opensource project Mozilla's PDF.js.

Download the necessary files, and integrate them into the app, on Andorid in the Assets folder and on UWP in the root directory.

We then create our Renderer by loading the PDF.js library into the WebView with the specified file.

In conclusion

Now that our control is ready, we can call it from any page:

To view the complete project code click here.