Reader Level:
ARTICLE

Silverlight 4.0 Feature #2: Printing Support

Posted by dj Articles | Visual Studio 2010 November 24, 2009
This article will introduce new printing support in Silverlight 4.0.
  • 0
  • 0
  • 5959

Objective

This article will introduce New Printing support in Silverlight 4.0.

Introduction

Silverlight 4.0 add support to enable business application development.  Printing support allows developer to print the Silverlight application itself.  Printing support helps

  1. Formatting the content of the Printing.
  2. Specifying the content of the Printing.
  3. Choosing the Printer.
How Printing support works?

While using the Printing support classes, eventually Silverlight application open the Printing Dialog box.

What is new in Silverlight 4.0 for printing support?

Answer is PrintDocument class. This class provides events and function to achieve Printing in Silverlight application. 

PrintDocument class
  1. This class is inside namespace System.Windows.Printing.
  2. There are three events to handles printing. They are EndPrint , StartPrint , PrintPage.
  3. There is one method called Print() to do the print operation.  This method takes no input parameter.
  4. There is a property DocumentName .
How to use this class?
  1. Create instance of PrintDocument class.
  2. Specify the content to print.
  3. Write code to handle the PagePrint event.
  4. Entire Silverlight control can be printed by setting PageVisual to layout.
  5. Portion of Silverlight application can be printed by setting the pagevisual tospecific control.
Diving to some example

I have a
  1. Stack Panel inside a Grid
  2. 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
  1. Create instance of PrintDocument class.
  2. Handling PrintPage event
  3. Inside the event setting PageVisual to name of the text box.
  4. Calling the Print() method in last.
  5. 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 

1.gif

On clicking of Print Button, Here you could select you Print options. 

2.gif 

Conclusion

In this article, I gave a basic introduction of Printing Support in Silverlight 4.0. Thanks for reading.

COMMENT USING

Trending up