Objective
This article will introduce New Printing support in Silver Light 4.0.
Introduction
Silver Light 4.0 add support to enable business application development. Printing support allows developer to print the Silver Light application itself. Printing support helps
- Formatting the content of the Printing.
- Specifying the content of the Printing.
- Choosing the Printer.
How Printing support works?
While using the Printing support classes, eventually Silver Light application opens the Printing Dialog box like below.
What is new in Silver Light 4.0 for printing support?
Answer is PrintDocument class. This class provides events and function to achieve Printing in Silver Light application.
PrintDocument class
- This class is inside namespace System.Windows.Printing.
- There are three events to handles printing. They are EndPrint , StartPrint , PrintPage.
- There is one method called Print() to do the print operation. This method takes no input parameter.
- There is a property DocumentName .
How to use this class?
- Create instance of PrintDocument class.
- Specify the content to print.
- Write code to handle the PagePrint event.
- Entire SilverLight control can be printed by setting PageVisual to layout.
- Portion of silverlight application can be printed by setting the pagevisual tospecific control.
Diving to some example
I have a
- Stack Panel inside a Grid
- Inside Stack Panel there is one button and a text box.
<Grid x:Name=”LayoutRoot” Background=”Azure”>
<StackPanel Height=”auto” Width=”auto” x:Name=”stkPanel” Orientation=”Horizontal” >
<TextBlock x:Name=”txtBlcok” Text=”Dhananjay Kumar” Height=”40″ Width=”175″ />
<Button x:Name=”mybutton” Content=”Print” Click=”mybutton_Click” Height=”30″ Width=”50″ /></StackPanel>
</Grid>
Now I need to,
Print the text Box
- Create instance of PrintDocument class.
- Handling PrintPage event
- Inside the event setting PageVisual to name of the text box.
- Calling the Print() method in last.
- If you want to print the entire application, just set PageVisual to any layout like stack or Grid. In this case for stack PageVisual would be set to stkPanel.
private
void mybutton_Click(object sender, RoutedEventArgs e)
{
PrintDocument prt = new
PrintDocument();
prt.PrintPage +=(s,args) =>
{
args.PageVisual = txtBlcok;
};
prt.Print();
}
Output
On clicking of Print Button, Here you could select you Print options.
Conclusion
In this article, I gave a basic introduction of Printing Support in Silver Light 4.0. Thanks for reading.
Leave a Reply