Printing Support in Silverlight 4
Posted by: Suprotim Agarwal ,
on 4/19/2010,
in
Category Silverlight 2, 3, 4 and 5
Abstract: Silverlight 4 is released and it comes with a whole set of new features. One of the new features added in Silverlight 4 is the support for printing. This was a much needed feature especially when creating business/enterprise applications. In this article, we will see how to use the printing API in Silverlight 4.
Silverlight 4 is released and it comes with a whole set of new features. One of the new features added in Silverlight 4 is the comprehensive support for printing with the ability to query page sizes and the printable area. This was a much needed feature especially when creating business/enterprise applications. In this article, we will see how to use the printing API in Silverlight 4.
If you have not yet installed Silverlight 4, then check this link Silverlight 4 Released for the tools required to build Silverlight 4 applications using Visual Studio 2010.
Step 1: Create a new Silverlight 4 application. Open Visual Studio 2010 > File > New project > Select Silverlight template from Visual C# or Visual Basic > Give a name and location to the project and click OK
In the ‘New Silverlight Application’ dialog box, keep the default options and click OK
Step 2: To keep this example simple, we will print an image. We will see advanced scenarios in the forthcoming articles.
The xaml of the application looks similar to the following:
<UserControl x:Class="PrintingSilverlight.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="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"
Height="400" Width="500">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="262*"/>
<RowDefinition Height="38*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ScrollViewer Margin="4,4.135,4,4" HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<Image x:Name="imgOne" Source="/PrintingSilverlight;component/Images/Desert.jpg"
Height="500" Width="500"/>
</ScrollViewer>
<Button x:Name="Print" Content="Print" Margin="4,5,4,0"
Click="Print_Click" Grid.Row="1" />
</Grid>
</UserControl>
Step 3: Now add the following code to the button click (Add a reference to System.Windows.Printing)
C#
private void Print_Click(object sender, RoutedEventArgs e)
{
PrintDocument pdoc = new PrintDocument();
pdoc.PrintPage += (p, args) => {
args.PageVisual = imgOne;
args.HasMorePages = false;
};
pdoc.EndPrint += (p, args) =>
{
MessageBox.Show("Printing operation completed");
};
pdoc.Print("Some Document");
VB.NET (Converted code)
Private Sub Print_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim pdoc As New PrintDocument()
AddHandler pdoc.PrintPage, Sub(p, args)
args.PageVisual = imgOne
args.HasMorePages = False
End Sub
AddHandler pdoc.EndPrint, Sub(p, args) MessageBox.Show("Printing operation completed")
pdoc.Print("Some Document")
End Sub
Here’s what we are doing.
- We first create a PrintDocument and attach a handler to the PrintPage event. This event occurs whenever each page gets printed.
- In the PrintPage event, we print the entire image control by setting the PageVisual property to the image control. Since there is only one page to be printed, we explicitly set the HasMorePages property to false.
- We then attach a handler to the EndPrint event. This event occurs when printing completes or when the printing operation is cancelled.
- A call is made to pdoc.Print() and a document name is passed. This name appears in the printer spooler.
Note: If the content of the image control is larger than the PrintableArea, it will be clipped.
Step 4: Run the application and hit the print button. If you print the document to an xps, you should get a similar output.
In the forthcoming articles, we will explore many more features of Silverlight 4. Stay tuned!
The entire source code of this article can be downloaded over here
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn