How to do print functionality in Silverlight ?



Hi

Silverlight4.0 support Printing Api, using this api we can do the print functionality in silverlight 4.0 like this
Step1: Create the XAML page design like this

<StackPanel>
<Image x:Name="imgOne" Source="Img/Winter.jpg"
Height="300" Width="500"/>

<Button x:Name="Print" Content="Print" Width="150" Height="40"
Click="Print_Click" />
</StackPanel>

Step2:Write the code on Click Event in Code behind page like this
and make ensure to use “System.Windows.Printing” namespace.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Printing;

namespace PrintTest
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}

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");

}
}
}

Advertisement

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.