Printing Support in Silverlight 4

Posted by: Suprotim Agarwal , on 4/19/2010, in Category Silverlight 2, 3, 4 and 5
Views: 42193
Abstract: Silverlight 4 is released and it comes with a whole set of new features. One of the new features added in Silverlight 4 is the support for printing. This was a much needed feature especially when creating business/enterprise applications. In this article, we will see how to use the printing API in Silverlight 4.
Silverlight 4 is released and it comes with a whole set of new features. One of the new features added in Silverlight 4 is the comprehensive support for printing with the ability to query page sizes and the printable area. This was a much needed feature especially when creating business/enterprise applications. In this article, we will see how to use the printing API in Silverlight 4.
If you have not yet installed Silverlight 4, then check this link Silverlight 4 Released for the tools required to build Silverlight 4 applications using Visual Studio 2010.
Step 1: Create a new Silverlight 4 application. Open Visual Studio 2010 > File > New project > Select Silverlight template from Visual C# or Visual Basic > Give a name and location to the project and click OK




Silverlight 4 Project
In the ‘New Silverlight Application’ dialog box, keep the default options and click OK
New Silverlight App
Step 2: To keep this example simple, we will print an image. We will see advanced scenarios in the forthcoming articles.
The xaml of the application looks similar to the following:
<UserControl x:Class="PrintingSilverlight.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Height="400" Width="500">
 
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="262*"/>
            <RowDefinition Height="38*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <ScrollViewer Margin="4,4.135,4,4" HorizontalScrollBarVisibility="Auto"
            VerticalScrollBarVisibility="Auto">
            <Image x:Name="imgOne" Source="/PrintingSilverlight;component/Images/Desert.jpg"
                   Height="500" Width="500"/>
        </ScrollViewer>
        <Button x:Name="Print" Content="Print" Margin="4,5,4,0"
                Click="Print_Click" Grid.Row="1" />
    </Grid>
   
</UserControl>
 
Step 3: Now add the following code to the button click (Add a reference to System.Windows.Printing)
C#
        private void Print_Click(object sender, RoutedEventArgs e)
        {
            PrintDocument pdoc = new PrintDocument();
            pdoc.PrintPage += (p, args) => {
                args.PageVisual = imgOne;
                args.HasMorePages = false;
 
            };
 
            pdoc.EndPrint += (p, args) =>
            {
                MessageBox.Show("Printing operation completed");
            };
 
            pdoc.Print("Some Document");
       
 
VB.NET (Converted code)
      Private Sub Print_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
                  Dim pdoc As New PrintDocument()
                  AddHandler pdoc.PrintPage, Sub(p, args)
                        args.PageVisual = imgOne
                        args.HasMorePages = False
 
                  End Sub
 
                  AddHandler pdoc.EndPrint, Sub(p, args) MessageBox.Show("Printing operation completed")
 
                  pdoc.Print("Some Document")
      End Sub
Here’s what we are doing.
-      We first create a PrintDocument and attach a handler to the PrintPage event. This event occurs whenever each page gets printed.
-      In the PrintPage event, we print the entire image control by setting the PageVisual property to the image control. Since there is only one page to be printed, we explicitly set the HasMorePages property to false. 
-      We then attach a handler to the EndPrint event. This event occurs when printing completes or when the printing operation is cancelled.
-      A call is made to pdoc.Print() and a document name is passed. This name appears in the printer spooler.
Note: If the content of the image control is larger than the PrintableArea, it will be clipped.
Step 4: Run the application and hit the print button. If you print the document to an xps, you should get a similar output.
Print Output
In the forthcoming articles, we will explore many more features of Silverlight 4. Stay tuned!
The entire source code of this article can be downloaded over here

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce the The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on .NET Standard and the upcoming C# 8.0 too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for ten consecutive times. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Daniel Craver on Monday, April 19, 2010 12:29 PM
Nice. I will stay tuned to explore the new features of Silverlight 4!
Comment posted by Silverlight Development India< on Wednesday, August 25, 2010 8:31 AM
there is very small difference Silverlight 4 and Silverlight 3. but Printing Support in Silverlight 4 is really good one...:)
Comment posted by Marc Roussel on Tuesday, October 5, 2010 5:16 PM
How do you print the content of an entire grid which is in a scrollviewer and on screen you don't see all the controls but if you scroll down you see more controls.
How would you go to print all the contant of that grid ?
Comment posted by Vijay on Friday, March 4, 2011 3:11 AM
Hi,
   How to print the grid values in silverlight?...
Comment posted by Juan on Friday, December 27, 2013 12:22 AM
how I get name printed install in the client on Silverligth?...I need is  there.

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

C# .NET BOOK

C# Book for Building Concepts and Interviews

Tags

JQUERY COOKBOOK

jQuery CookBook