Reader Level:

Print Document in Silverlight

By Priya Linge on Nov 29 2011
This article demonstrate the print document in Silverlight.
    • Like
    • Love It
    • Awesome
    • Interesting
    • It's Okay
    • Thumbs Down
  • 22.8k
  • 0

How we can print the document in a Silverlight application.

Step 1: We have the PrintDocument Class which defines a reusable object that sends output to a printer.

  1. PrintDocument : The PrintDocument object encapsulates all the information needed to print a page. They associate with the control which content can be print. They handle the events and operations of printing.

    Namespace : System.Drawing.Printing.PrintDocument


    public class PrintDocument : Component
  2. We can create an instance of the PrintDocument class, set the properties that describe how to print, and call the Print method to start the printing process. Handle the PrintPage event where you specify the output to print, by using the Graphics included in the PrintPageEventArgs.
  3. Associate control to Print document

    private void printDoc_PrintPage(object sender, PrintPageEventArgs e)
         // print current page
        e.PageVisual = printPage;


Step 2: Create one user control page name as PrintPage.Xaml and design header and footer in this user control page like as following.

<Grid x:Name="LayoutRoot" Background="White">
            <RowDefinition Height="Auto" />
            <RowDefinition />
            <RowDefinition Height="Auto" />
            <TextBlock Text="HEADER" />
        <ItemsControl Name="BodyItemsControl" Grid.Row="1" Margin="0,24" />

        <ItemsControl Name="TemplateItemsControl">
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />

                        <TextBlock Text="{Binding ID}" Margin="2" />
                        <TextBlock Text=" - " Grid.Column="1" Margin="2" />
                        <TextBlock Text="{Binding Description}" Grid.Column="2"
                                TextWrapping="Wrap" MaxWidth="500"
                                HorizontalAlignment="Left" Margin
="2" />

        <Grid Grid.Row="2">
            <TextBlock Text="FOOTER" />

Step 3: In MainPage.Xaml create an instance of PrintDocument like as following.

public MainPage()


        this.Loaded += new RoutedEventHandler(MainPage_Loaded);

void MainPage_Loaded(object sender, RoutedEventArgs e)
            printDoc.PrintPage +=
                new EventHandler<PrintPageEventArgs>(printDoc_PrintPage);
//following items for printing.
private void GetItems()
            for (int i = 0; i < 100; i++)
                    new Item()
                        ID = i,
                        Description = "This is Print Document " + i

//Handling the event when we're printing:

private void printDoc_PrintPage(object sender, PrintPageEventArgs e)
    PrintPage printPage = new PrintPage();
          // print current page
    e.PageVisual = printPage;
e.HasMorePages = true;
} Step 4: Add a button to the MainPage.Xaml and print the document when the button is clicked:

<Button Content="Button" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="42,56,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />

private void Button_Click(object sender, RoutedEventArgs e)
    printDoc.Print("Printing A Page");
} Step 5: Output look like as followingPrnySil1.gif PrnySil2.gif