Click here to Skip to main content
13,195,163 members (66,823 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

13.1K views
3 bookmarked
Posted 25 Aug 2010

Exploring Silverlight 4 - Part I (The Printing API)

, 25 Aug 2010
Rate this:
Please Sign up or sign in to vote.
Exploring Silverlight 4 - the Printing API

One of the major peeves that Silverlight developers had in its earlier versions was the lack of printing support. Printing is an essential feature of many applications and more so in the case of LOB applications. Hence, when Silverlight 4 was released with a full featured Printing API, developers welcomed it with open arms.

What’s Included in the Printing API

The System.Windows.Printing namespace

This namespace provides printing services for a Silverlight application through its various classes.

Class NameWhat It Does
PrintDocumentProvides printing capabilities for a Silverlight application
PrintPageEventArgsProvides data for the PrintPage event
BeginPrintEventArgsProvides data for the BeginPrint event
EndPrintEventArgsProvides data for the EndPrint event

PrintDocument Class

The commonly used methods, properties, and events of this class are as follows:

MethodDescription
PrintStarts the printing process for the specified document by opening the print dialog box
PropertyDescription
PrintedPageCountGets the number of pages that have printed
EventDescription
BeginPrintOccurs after the Print() method is called and the print dialog box successfully returns, but before the PrintPage event is raised
EndPrintOccurs when the printing operation is complete or when the print operation is canceled by the application author
PrintPageOccurs when each page is printing

PrintPageEventArgs Class

The commonly used properties of this class are:

NameDescription
HasMorePagesRetrieves or specifies whether there are more pages to print
PageMarginsRetrieves the margins of the page that are currently printing
PageVisualRetrieves or specifies the visual element to print
PrintableAreaRetrieves the size of the printable area

Basic Steps for Adding Printing Functionality in a Silverlight Application

  1. Add a Print button or a Print menu option through which user can initiate a print operation.
  2. Create an instance of PrintDocument in the application
  3. Call the Print() method to open a print dialog box
  4. Write code for the PrintPage event handler for PrintDocument to perform the print operation

You have several options for printing: print the entire Silverlight control, print part of a control, and so forth.

If you set the PageVisual property to the layout root of the Silverlight content, such as a Grid, you can print the entire control. If you set PageVisual to a particular control or UIElement, you can print only that Silverlight control.

The HasMorePages property can be used to print multiple pages.

Enough said about the theory, let’s now move on to some examples.

Example

Assume that you have created a TreeView application and want to print the entire content of the grid. A button, Print, is added to the page. The markup for MainPage.xaml is as follows:

Listing 1

<UserControl x:Class="TreeViewPrint.MainPage"

xmlns="http://schemas.microsoft.com/winfx/
2006/xaml/presentation"

xmlns:x=
Technorati Tags: silverlight, silverlight 4, printing

"http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:d="http://schemas.microsoft.com/expression
/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

mc:Ignorable="d"

d:DesignHeight="300" d:DesignWidth="400"

xmlns:

sdk="http://schemas.microsoft.com/winfx/
2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" Background="LightSeaGreen">
<sdk:TreeView Width="150" Height="150" 

Background="Beige" Name="techTree">
<sdk:TreeViewItem Header="Microsoft Technology" 

IsExpanded="True" >
<sdk:TreeViewItem Header=".NET" IsExpanded="True">
<sdk:TreeViewItem Header="C#" >
<sdk:TreeViewItem Header="3.0">
<sdk:TreeViewItem Header="4.0">
</sdk:TreeViewItem>
</sdk:TreeViewItem>
</sdk:TreeViewItem>
</sdk:TreeViewItem>
</sdk:TreeViewItem>
</sdk:TreeView>
<Button Content="Print" Height="23" 

HorizontalAlignment="Left" Name="btnPrint" 

    Width="75" Click="btnPrint_Click" />
</Grid>
</UserControl>

The markup creates a TreeView control, adds TreeViewItems to it, and creates a button with caption, Print.

In the code-behind, write the following code:

Listing 2

private void btnPrint_Click(object sender, RoutedEventArgs e)
{
PrintDocument prnt = new PrintDocument();
prnt.PrintPage += new
EventHandler<PrintPageEventArgs>(prnt_PrintPage);
prnt.Print("TreeView Application");
}
void prnt_PrintPage(object sender, PrintPageEventArgs e)
{
e.PageVisual = LayoutRoot;
}

The above code is simple and self-explanatory. To print only the tree and not the whole grid, you can fine tune the above code as follows.

Listing 3

void prnt_PrintPage(object sender, PrintPageEventArgs e)
{
e.PageVisual = techTree;
}

Tip: To print a Silverlight 4 RichTextBox without a surrounding border, set BorderBrush="{x:Null}".

You can add headers and footers to the page by using TextBlock controls. The PrintableArea property of PrintPageEventArgs enables you to set the print area.

There is a lot more to explore in the printing API but this is it from me for now. With time, there will be more to come.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mamta D
India India
Mamta has been working with .NET technologies for quite some years. Enthusiastic about Silverlight and WPF, she hopes to explore more of these and share her knowledge with others.

You may also be interested in...

Pro

Comments and Discussions

 
GeneralMy vote of 5 Pin
Uday Kumar B R15-Jan-12 0:33
memberUday Kumar B R15-Jan-12 0:33 
Generalpagination in treeview control in silveright 4 Pin
maykalasunder23-Sep-10 23:14
membermaykalasunder23-Sep-10 23:14 
GeneralRe: pagination in treeview control in silveright 4 Pin
mamta_m_d23-Sep-10 23:26
membermamta_m_d23-Sep-10 23:26 
GeneralRe: pagination in treeview control in silveright 4 Pin
maykalasunder28-Sep-10 4:19
membermaykalasunder28-Sep-10 4:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171019.1 | Last Updated 25 Aug 2010
Article Copyright 2010 by Mamta D
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid