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>
Step 3: In MainPage.Xaml create an instance of PrintDocument like as
following.
public MainPage()
{
InitializeComponent();
this.Loaded
+= new
RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
GetItems();
printDoc.PrintPage +=
new EventHandler<PrintPageEventArgs>(printDoc_PrintPage);
}
//following items for printing.
private void GetItems()
{
for (int i = 0; i < 100; i++)
{
items.Add(
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;
break;
}
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 following