Print Document In Silverlight

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

    [C#]

    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
    1. private void printDoc_PrintPage(object sender, PrintPageEventArgs e) {  
    2.     // print current page  
    3.     e.PageVisual = printPage;  
    4. }  

Step 2

Create one user control page name as PrintPage.Xaml and design header and footer in this user control page like as following.
  1. <Grid x:Name="LayoutRoot" Background="White">  
  2.     <Grid.RowDefinitions>  
  3.         <RowDefinition Height="Auto" />  
  4.         <RowDefinition />  
  5.         <RowDefinition Height="Auto" />  
  6.     </Grid.RowDefinitions>  
  7.     <!--Header-->  
  8.     <Grid>  
  9.         <TextBlock Text="HEADER" />  
  10.     </Grid>  
  11.     <!--Body-->  
  12.     <ItemsControl Name="BodyItemsControl" Grid.Row="1" Margin="0,24" />  
  13.     <ItemsControl Name="TemplateItemsControl">  
  14.         <ItemsControl.ItemTemplate>  
  15.             <DataTemplate>  
  16.                 <Grid>  
  17.                     <Grid.ColumnDefinitions>  
  18.                         <ColumnDefinition Width="Auto" />  
  19.                         <ColumnDefinition Width="Auto" />  
  20.                         <ColumnDefinition Width="*" />  
  21.                     </Grid.ColumnDefinitions>  
  22.                     <TextBlock Text="{Binding ID}" Margin="2" />  
  23.                     <TextBlock Text=" - " Grid.Column="1" Margin="2" />  
  24.                     <TextBlock Text="{Binding Description}" Grid.Column="2" TextWrapping="Wrap" MaxWidth="500" HorizontalAlignment="Left" Margin="2" />  
  25.                 </Grid>  
  26.             </DataTemplate>  
  27.         </ItemsControl.ItemTemplate>  
  28.     </ItemsControl>  
  29.     <Grid Grid.Row="2">  
  30.         <TextBlock Text="FOOTER" />  
  31.     </Grid>  

Step 3

In MainPage.Xaml create an instance of PrintDocument like as following.
  1. public MainPage() {  
  2.     InitializeComponent();  
  3.     this.Loaded += new RoutedEventHandler(MainPage_Loaded);  
  4. }  
  5. void MainPage_Loaded(object sender, RoutedEventArgs e) {  
  6.     GetItems();  
  7.     printDoc.PrintPage += newEventHandler < PrintPageEventArgs > (printDoc_PrintPage);  
  8. }  
  9. //following items for printing.  
  10. privatevoid GetItems() {  
  11.     for (int i = 0; i < 100; i++) {  
  12.         items.Add(newItem() {  
  13.             ID = i,  
  14.                 Description = "This is Print Document " + i  
  15.         });  
  16.     }  
  17. }  
  18. //Handling the event when we're printing:  
  19. private void printDoc_PrintPage(object sender, PrintPageEventArgs e) {  
  20.     PrintPage printPage = new PrintPage();  
  21.     // print current page  
  22.     e.PageVisual = printPage;  
  23.     e.HasMorePages = true;  
  24.     break;  
  25. }   
Step 4
 
Add a button to the MainPage.Xaml and print the document when the button is clicked,

  1. <Button Content="Button" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="42,56,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />  
  2. private void Button_Click(object sender, RoutedEventArgs e)  
  3. {  
  4.     printDoc.Print("Printing A Page");  
  5. }  
Step 5
 
Output look like as following,
PrnySil1.gif

PrnySil2.gif


Similar Articles