Printing in Silverlight

14 07 2014

Printing in Silverlight

Printing is a very basic requirement for business application now days. At the advent of Silverlight there was no direct support for printing. But in Silverlight version 4 Microsoft have provided Print Document assembly for achieving the printing functionality.

What if the application still uses Silverlight version 3.0 or earlier versions?

In this article I will discuss the alternate approach for printing in Silverlight version 3.0 and earlier and then move on to a more sophisticated approach using Print Document provided in Silverlight 4.0.

The alternative is bit simple but not that much mature. Here we will take the entire image of the control that needs to be printed and let the user to save it using the save dialog. But one major problem with this approach is related to scaling of the image. In a case that have a large number of records to be printed like 100 records in data grid than the height of the jpeg produce would be very high and when it would be printed then it would automatically scale down the size of the image to fit in one single print page. The clarity for the scaled down image would not be good enough for reading purpose. So to overcome this thing I developed an algorithm to split the big heighted image into fixed slices and then I have used PdfSharp library to send the each sliced images to separate pages in pdf file. Finally the user will be able to save the Pdf file from where he can print the document. But here the chances to cut down the data when the image is splitted would remain.

Due to such pain and alternate unorganized approaches Microsoft finally Gifted Silverlight 4.0 with Print Document class. This class provides printing capabilities in Silverlight Applications. Refer http://msdn.microsoft.com/en-us/library/system.windows.printing.printdocument(VS.95).aspx for more details.

I strived hard to develop one such common framework related to print document class till I found one fantastic article by Pete Brown in his blog Creating a Simple Report Writer in Silverlight 4 http://10rem.net/blog/2010/05/09/creating-a-simple-report-writer-in-silverlight-4

This framework allows printing for one item source with one data template. My requirement forced me to print multiple grids at the same time in one single print job. So I analyzed the framework and with some modifications finally achieved it. I have converted some of its properties to arrays which use combination of multiple Data Templates and Item Sources to print multiple grids in one single print job.

Thanks Peter for such a fabulous use of Print Document.

Advertisements