Software Architecture-Silverlight 4.0-New Feature-Printing Option

In Silverlight 4.0, new printing class is provided in named PrintDocument in namespace of System.Windows.Printing, which help to print any UIElement and its content (Childs) of Visual tree.

This class has three main events; First PrintPage which helps to identify what is the content to be printed via PrintPageEventArgs.PageVisual. Second BeginPrint helps to do some action before printing actually starts. Third EndPrint help to some action after printing actually ends.

A sample code will look like as:

  public partial class PrintPage : Page
        PrintDocument printD;
        public PrintPage()
            printD = new PrintDocument();
            printD.PrintPage += new EventHandler<PrintPageEventArgs>(printD_PrintPage);
            printD.BeginPrint += new EventHandler<BeginPrintEventArgs>(printD_BeginPrint);
            printD.EndPrint += new EventHandler<EndPrintEventArgs>(printD_EndPrint);

        void printD_EndPrint(object sender, EndPrintEventArgs e)
            MessageBox.Show("Printing Ended.......");

        void printD_BeginPrint(object sender, BeginPrintEventArgs e)
            MessageBox.Show("Printing Started.......");

        void printD_PrintPage(object sender, PrintPageEventArgs e)
            MessageBox.Show("Height: " + e.PrintableArea.Height + "Width: " + e.PrintableArea.Width);
            e.PageVisual = PrintArea;

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        private void button1_Click(object sender, RoutedEventArgs e)
            printD.Print("Test Page");


XAML will look like as:

<navigation:Page x:Class="ImplicitThemeProc.PrintPage" 
           d:DesignWidth="640" d:DesignHeight="480"
           Title="PrintPage Page">
    <Grid x:Name="LayoutRoot">
        <StackPanel Name="PrintArea" Height="400" Margin="264,40,170,40">
            <TextBlock  Text="Hellllo Sarwar......." FontSize="16"></TextBlock>
            <TextBlock Text="Hellllo Sarwar......." FontSize="16"></TextBlock>
            <TextBlock Text="Hellllo Sarwar......." FontSize="16"></TextBlock>
        <Button Content="Print......" Height="23" HorizontalAlignment="Left" Margin="104,131,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />

Printed page will look like as:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: