Silver Light 4.0 Feature #1 Printing Support

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

  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 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

  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

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.

One response to “Silver Light 4.0 Feature #1 Printing Support”

  1. “Silver Light 4.0 Feature #1 Printing Support | debug mode.
    …..” ended up being a good post. However, if it owned
    alot more pictures it would definitely be possibly even a lot better.
    Cya ,Mario

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com