
Several times,this question has been asked that how to print a page in Silverlight, thanks to Microsoft who have given this print support in Silverlight 4. Before going to start you must have installed Visual Studio 2010 and Silverlight 4.Let’s start.
Open your Visual Studio 2010, from File -> New Project -> Select Silverlight template -> give a name and click OK button to continue.
Following is the code of Main.xaml file.<UserControl x:Class="Silverlight4_Printing.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" mc:Ignorable="d" d:DesignHeight="400" d:DesignWidth="560" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> <Grid x:Name="LayoutRoot" Background="White" HorizontalAlignment="Center"> <sdk:Label Height="28" HorizontalAlignment="Left" Margin="10,10,0,0" Name="label1" VerticalAlignment="Top" Width="297" /> <Button Content="Print" Height="23" HorizontalAlignment="Left" Margin="66,103,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> </Grid></UserControl>Following is the code of Main.xaml.cs file public MainPage() { InitializeComponent(); label1.Content = "Welcome to aspxtutorial.com"; } private void button1_Click(object sender, RoutedEventArgs e) { var document = new PrintDocument(); document.PrintPage += (s, args) => { args.PageVisual = LayoutRoot; args.HasMorePages = false; button1.Visibility = Visibility.Collapsed; }; document.EndPrint += (s, args) => { button1.Visibility = Visibility.Visible; }; document.Print("Silverlight Print Page"); }
Output
Download
Silverlight 4 - Printing page.zip (252.13 kb)
See live demo