Monday, March 22, 2010

Silverlight 4: How to use the new Printing API ?

Silverlight 4 now supports printing functionality using the Printing APIs. Using the API’s you can now print your whole application screen or a portion of the application. Also, you can customize the look while you printing your part/full application. In this post I will step you guys to the depth of the printing API.

Prerequisite

Before you start working with the Silverlight 4 printing API, your environment should match the following prerequisite:

   * You are using Windows XP SP2 or higher version of operating system
   * You already installed Visual Studio 2010 RC
   * Latest Silverlight 4 Tools for Visual Studio 2010

If you meet the above prerequisite, we are ready to go to the next step.

Setting up the Silverlight 4 Project

Let us start with a new blank Silverlight application project. Open Visual Studio 2010 and click on File –> New –> Project or just press CTRL + SHIFT + N to open up the new project dialog. Now expand the “Visual C#” section and select “Silverlight”. From the right pane select “Silverlight Application”, chose the location to create the project and give a proper name (here I am using “Silverlight4.PrintingAPI.Demo” as the project name). Click “ok” which will bring up another dialog. Select “Silverlight 4” & hit ok to create the blank Silverlight application.

XAML Design

Visual Studio will automatically create a “MainPage.xaml” for you and display inside your Visual Studio IDE. You can now design your application as per your need. Let us add some contents inside the page:

<Canvas x:Name="cnvContainer">
   <Border BorderBrush="#FFC7851A" BorderThickness="1" Height="118" HorizontalAlignment="Left" Margin="12,26,0,0" Name="border1" VerticalAlignment="Top" Width="376" Background="#42F5E0A7" />
   <TextBlock Text="Silverlight 4 Printing API Demo" FontWeight="Bold" HorizontalAlignment="Left" FontSize="20" Margin="26,41,0,225" Width="353" />
   <ProgressBar Height="18" HorizontalAlignment="Left" Margin="26,108,0,0" Name="progressBar1" Value="75" VerticalAlignment="Top" Width="353" />
</Canvas>
<Button Content="Print" Height="23" HorizontalAlignment="Left" Margin="166,244,0,0" Name="btnPrint" VerticalAlignment="Top" Width="75" Click="btnPrint_Click" />

Here I am adding a “Border”, a “TextBlock” a “ProgressBar” and a “Button” inside the main Grid panel “LayoutRoot” inside the “MainPage.xaml”. We will start our printing job once we click on the button. Hence, added the Click event with the button.

Read more: .net Funda

Posted via email from jasper22's posterous