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.
- 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
- 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.
- 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">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto" />
- <RowDefinition />
- <RowDefinition Height="Auto" />
- </Grid.RowDefinitions>
- <!--Header-->
- <Grid>
- <TextBlock Text="HEADER" />
- </Grid>
- <!--Body-->
- <ItemsControl Name="BodyItemsControl" Grid.Row="1" Margin="0,24" />
- <ItemsControl Name="TemplateItemsControl">
- <ItemsControl.ItemTemplate>
- <DataTemplate>
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="Auto" />
- <ColumnDefinition Width="Auto" />
- <ColumnDefinition Width="*" />
- </Grid.ColumnDefinitions>
- <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>
- </DataTemplate>
- </ItemsControl.ItemTemplate>
- </ItemsControl>
- <Grid Grid.Row="2">
- <TextBlock Text="FOOTER" />
- </Grid>

Gui Seok ChoiPosted Feb 2, 2012, 12:45 AM
It's really helpful to me.