Introduction to the Silverlight Print Option

This article looks at how to implement the print option in a Silverlight application. This print feature is bundled along with the 4.0 version of Silverlight SDK. Sample source codes are provided in the following sections.

What's the Advantage of Silverlight's Print Functionality?

In most of the web based technologies there is no core support available for the print functionality. For example, if you consider an application there is no straight way to implement a print functionality. Even if there is one available like that of a Javascript print, the developer does not have good control over it. Now Silverlight 4.0 has come up with a built-in print functionality, which leverages for the developer to perform the below print tasks.

1.Print the whole Silverlight page content.

2.Print only a part of the Silverlight content.

3.Printing multiple pages including pagination.

Print Functionality Implementation

The printing in Silverlight can be achieved through the PrintDocument class, which is ingrained in the namespace System.Windows.Printing. PrintPage event should be hooked up to the PrintPageEventHandler where the Silverlight content to be printed is set. Below is the basic implementation of the print functionality.

XAML code:

<UserControl x:Class="PrintingFeatureDemo.MainPage"
    d:DesignHeight="409" d:DesignWidth="768">
    <Grid x:Name="LayoutRoot" Background="White">
            <TextBlock Name="PrintTextBlock" Text="Text to be printed" FontSize="20" Margin="239,12,337,366"></TextBlock>
            <Image Name="PrintImage" Source="Chrysanthemum.jpg" Width="100" Height="100" Margin="286,59,382,250"></Image>
            <Button Content="Print All" Height="23" HorizontalAlignment="Left" Margin="302,181,0,0" Name="PrintAllButton" VerticalAlignment="Top" Width="75" Click="PrintAllButton_Click" />

The above XAML code contains a TextBlock and an Image, which should get printed by clicking on the Print button.

C# code:

namespace PrintingFeatureDemo
    public partial class MainPage : UserControl
        PrintDocument _printAll;
        public MainPage()
            _printAll = new PrintDocument();
            //Hooks up the PrintPageevent
            _printAll.PrintPage += new EventHandler7lt;PrintPageEventArgs>(_printDocument_PrintPage);
        void _printDocument_PrintPage(object sender, PrintPageEventArgs e)
            //Sets the Silverlight section to be printed 
            e.PageVisual = this;
        private void PrintAllButton_Click(object sender, RoutedEventArgs e)
            _printAll.Print("Silverlight Content To Be Printed");            

In the above code, notice that the PrintPage event of the PrintDocument object is used to set the PageVisual, which is nothing but the Silverlight content to be printed. The Print method pops up the print dialog box, which in turn kicks off the printing process.

The above code would print the entire Silverlight content. If you need to print only a fragment of the Silverlight content then set the PageVisual property appropriately. For example, say on clicking on print you would like to print only the image, then set the PageVisual as shown in the below code.

void _printDocument_PrintPage(object sender, PrintPageEventArgs e)
            //Sets the Silverlight section to be printed 
            //This sets only the image as the content to be printed
            e.PageVisual = this.PrintImage;

Notable Properties and Events

In this section let us explore the important events and properties which seem to be pretty useful.

BeginPrint and EndPrint Events

These events helps the developer in handling the pre and post print events like setting up and tearing up the print environment. Below is the sample implementation.

_printDocument.BeginPrint += new EventHandler(_printAll_BeginPrint);
_printDocument.EndPrint += new EventHandler(_printAll_EndPrint);
//Invoked when the print operation completes
void _printAll_EndPrint(object sender, EndPrintEventArgs e)
            //Check for the printing error
            if (e.Error != null)
                MessageBox.Show(e.Error.Message);//Display the error message
                MessageBox.Show("Printing successful");
//Kicked off when the Print button on the print dialog is clicked
void _printAll_BeginPrint(object sender, BeginPrintEventArgs e)
            //Setting up operations, if at all anything required

HasMorePages Property

This is a property of the PrintPageEventArgs class. Set this to true incase multiple pages need to be printed until you reach the final page where this property should be set to false. This property is the key while implementing multiple page print functionality.

Fig 1.0 is a sample screenshot of the print dialog.

The SharePoint print dialog
Figure 1: The SharePoint print dialog


I hope this article brings out the information about implementing the print functionality in a Silverlight application. Please make use of the comments section in order to provide your thoughts.

Thanks for reading the article.

About the Author

V.N.S Arun

I work for an MNC in Bangalore, India. I am fond of writing articles, posting answers in forums and submitting tips in dotnet. To contact me please feel free to make use of the "Send Email" option next to the display name.

Related Articles


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds