Printing PDF’S and Images Xamarin.Forms

Hey Guys!

Recently I had the need to implement a Printing feature for one of my Xamarin Forms projects, and discovered it was actually kind of straight forward to do, which brings me to our article of this week!

Right now Xamarin Forms does not support printing out of the box, but the good news is that each native platform already has support for this feature and we just need to tap into each native library and expose this feature for our App.

Prerequisites

You will need previous knowledge on the following subjects to follow along with the sample project in this article (all of these topics have been discussed on my previous articles, you can revisit them to refresh any doubts you might have while reading this article):

  • MVVM Pattern
  • Using Fody Property Change Plugin
  • Commanding
  • Services
  • Prism

Let’s get Started!

Now that we know we need to expose the native feature to our Xamarin Forms project, we immediately think on creating a service and consuming it with Dependency Services to get our native print services live, and to do so we will need the following in our app:

  • IPrintService Interface.
  • PrintService Implementation for Xamarin.Android and Xamarin.iOS.
  • Embedded PDF Resource File which we will print.
  • James Montemagno CurrentActivity, Media and Permissions Plugins.

IPrintService Interface

We will create a Services Directory in our project and inside of it, we are going to create IPrintService.cs and declare 2 methods in our interface, PrintImage(Stream img) and PrintPdfFile(Stream file):

Now that we have our Interface created, we just need to implement it in each platform, we will start by implementing the PrintImage feature.

Android PrintService:

In Android, we have a method available that allows us to print a Bitmap directly, so to simplify and abstract the method we decided to receive our data as a Stream and simply convert it to a bitmap and print.

iOS PrintService:

In iOS we do something similar to Android but with an UIImage, once we have our UIImage created we can then prepare our printer for the printing job and print right away.

Goodjob! now we should be able to print our images on AirPrint enabled printers, now it’s time to add our logic for printing PDF’s, after searching the docs and looking around if someone has done a sample for this I found that in the Syncfusion blog they have a great post that goes according to the official documentation in Android and iOS so I decided to try it out and it worked like a charm and decided to integrate it, Kudos to them for the good work!

Android PrintService:

For printing files in Android it’s a bit different than printing a Bitmap, because we will need to handle the printing lifecycle with a custom print adapter but what is a custom print adapter well Android docs states the following:

A print adapter interacts with the Android print framework and handles the steps of the printing process. This process requires users to select printers and print options before creating a document for printing. These selections can influence the final output as the user chooses printers with different output capabilities, different page sizes, or different page orientations. As these selections are made, the print framework asks your adapter to lay out and generate a print document, in preparation for final output. Once a user taps the print button, the framework takes the final print document and passes it to a print provider for output.

Android Printing Custom Docs

So once we have our PrintAdapter we simply pass it to our PrintManager and Android will handle all the remaining steps of the process to allow our users to print.

https://developer.android.com/training/printing/custom-docs.html

iOS PrintService:

For iOS we simply make a copy of our stream in a MemoryStream and set up the basic printer options to define stuff like the OutType of the print we want, for example, Grayscale, if it’s just a Photo if it’s General which includes images, text, and graphics. For our case we choosed General.

Ce finito Guys! 🥳🙌 Now our app should be able to print images and PDF’s on wifi, and AirPrint Enabled printers, you can do so much more with these native libraries so if you would like to learn more I definitely recommend reading more and checking out the xamarin-samples repo on GitHub some of them are made by the one and only Miguel Icaza🙌🙈, and official documentation for each platform.

Bonus:

  • Try updating the app to use the File Picker plugin and choose your PDF’s dynamically.

PDT: Limitations on PDF Printing in Android, I noticed the PDF’s must be version 1.6+ to be able to print in Android, anything below that Android PrintManager will say the PDF is malformed and can’t print.

Full Source Code for the sample App can be found on my Github Repo [FormsPrintSample]

Made with ❤ by Pujolsluis

Good References to learn more about Printing in iOS and Android

Xamarin-samples repo:

https://github.com/xamarin/ios-samples

https://github.com/xamarin/monodroid-samples

Syncfusion Printing PDF’s blog post:

https://www.syncfusion.com/kb/8767/how-to-print-pdf-documents-in-xamarin-forms-platform

Android Printing Official Documentation:

https://developer.android.com/training/printing

iOS Printing Official Documentation:

https://developer.apple.com/airprint/

Credits for artwork components:
Mobile Icons for the banner of the post made by Freepik

(Visited 8,258 times, 1 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.