Hello Friends, Last time we saw how to create make a PDF view and a thumb view for iOS and use it to open a PDF in Xamarin Forms. Now we are going to see how to do the same thing with Android. In brief, we will create a custom renderer based on Radaee PDF’s PDF view and thumb view. The creation process of these views includes some tricks I’ll share in this article.

Getting Started with Radaee PDF for Android

RadaeePDF is a library that provides PDF manipulation functionalities to mobile apps. It exists both for Android and iOS. It isn’t a free library, and its documentation isn’t very detailed. But on the other side, It is available as a one-time license (And not per user license as what several PDF solutions offer), and it has Xamarin Bindings available on Github.

This library isn’t available as a NuGet package. The team in charge of this lib only made a project with Xamarin Android and Xamarin iOS bindings on Github. So, you might need to know a little bit about creating and building Xamarin Native bindings Libraries (Check this doc out to learn about binding native libraries) if you find difficulties generating the dll on your own.

  • Reference the Radaee library into your Xamarin.Android project. You can get the library as I mentioned above.
  • Contact the RadaeePDF team and get a license if you need it,
  • Once you get the api keys, open your main activity, and add the following code in the “OnCreate” method after initializing Xamarin Forms

Building Android’s PDF View with Radaee PDF

We will now proceed and create the custom PDF control needed to open a PDF in Xamarin Forms Pages. For this, we will use Custom renderers. To learn more about custom renderers, you can follow this link.

  • First, in our shared project, we create a PDFReaderView, that will serve as the basis of our custom renderer. The code for this class is found in the previous article.
  • Now, in the Android project, we will build our custom renderer based on Radaee’s PDFLayoutView. But before we do that, we need to wrap this PDFLayoutView inside a custom android view built with xml, because the PDFLayoutView needs inflation.
    • Create an xml layout, with a container that contains the PDFLayout view as its only view.
    • Then create a class that inflates this layout, and gets the PDFLayout, and exposes it as a property. You can see all of these below:
  • We then create our custom renderer with the wraper control we just created above.
  • In our custom renderer, we configure the exposed pdflayoutview above.

Building the Thumb view

The process of building the thumb view is very similar to the one described above.

  • We create a wrapper view, to inflate the thumbview
    • Firist, we make the xml layout of the wrapper view
    • Second, we create a class to inflate this.
If this post was useful to you, follow me on Twitter,  Github, Linkedinor like my Facebook page to stay updated.Follow me on social media and stay updated
  • We use this wrapper view to create our custom renderer

Using these controls in the shared project is easy. Inside your XAML page, you will just need to reference the namespaces, and call the controls as follows.

Conclusion

We saw how to build a PDF reader control, and implant it into our Xamarin Forms pages. This can be handy for specific scenarios. Today we used Radaee PDF, but later I might share a post about another library, that is more stable, and free. If you want to build a custom PDF control and thumb view on iOS, you can do so with PDFKit this blog post will show you how.

Follow me on social media and stay updated
%d bloggers like this: