MULTIMEDIA

Building LOB Applications : Printing in a Silverlight LOB Application

3/19/2011 4:47:54 PM

1. Problem

You need to support printing in your Silverlight application.

2. Solution

Take advantage of the new printing support available in Silverlight 4.

3. How It Works

Users can leverage the browser printing capabilities to print Silverlight applications in Silverlight 3 but there are many situations where LOB applications need to have customized printing. Silverlight 4 includes the new PrintDocument class to provide printing capabilities to Silverlight applications.

After you add a PrintDocument object to the application, you can call the Print() method in a Button event handler. It is required that all dialog boxes in Silverlight must be user-initiated, otherwise a SecurityException will occur.

To perform the print operation, you handle the PrintPage event for the PrintDocument object. In the PrintPage event handler, set the PrintPageEventArgs.PageVisual property to the root UIElement that you want to print. So, if you want to print a single object like a DataGrid, you can set it on the PageVisual property. If you want to print a whole set of controls configured on a Grid, you set the PageVisual property to the Grid and all of its child controls will be printed as well.

To print multiple pages, you can set the PrintPageEventArgs.HasMorePages to true and the PrintPage event will fire again until HasMorePages is set to false. If you need to print a multi-page document, you can handle the BeginPrint event where you can page the data in a multipage document to the next page so that it is ready to be printed when PrintPage fires. You can perform post-printing clean up as well as check for errors in the EndPrint event.

4. The Code

To add printing support, you add a using clause for the System.Windows.Printing namespace. Next, declare an instance of the PrintDocument class and a few variables to keep track of current printing (currentpagePrinting) page and the page (savedPageNum) you should reset to after printing.

You wire up the PrintPage event to your PrintDocument variable, which is where most of the action happens. In the PrintPage event handler, you set the PageVisualCustomersDataGrid. Next, you have some logic to page through the data in order to print out all of the records. Listing 1 has the code. value to the

Listing 1. The Recipe 10 MainPage.Xaml.cs File
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Printing;

namespace Ch09_LOBApplications.Recipe9_10
{


public partial class MainPage : UserControl
{
PrintDocument pd = new PrintDocument();
int currentpagePrinting = 1;
int savedPageNum = 0;

public MainPage()
{
InitializeComponent();
CustomerDataForm.CommandButtonsVisibility =
DataFormCommandButtonsVisibility.All;

pd = new PrintDocument();
pd.PrintPage +=
new EventHandler<PrintPageEventArgs>(pd_PrintPage);
}

void pd_PrintPage(object sender, PrintPageEventArgs e)
{
e.PageVisual = CustomersDataGrid;
if (currentpagePrinting <= CustomerPager.PageCount)
{
e.HasMorePages = true;
currentpagePrinting++;
CustomerPager.PageIndex = currentpagePrinting;
}
else
{
e.HasMorePages = false;
CustomerPager.PageIndex = savedPageNum;
}
CustomersDataGrid.UpdateLayout();
}

private void ButtonCommitToServer_Click(object sender,
RoutedEventArgs e)
{
if (CustomersDomainDataSource.HasChanges &&
!CustomersDomainDataSource.IsBusy)
CustomersDomainDataSource.SubmitChanges();
}

private void PrintButton_Click(object sender, RoutedEventArgs e)
{
pd.Print("Customer List");


currentpagePrinting = 1;
savedPageNum = CustomerPager.PageIndex;
CustomerPager.PageIndex = 1;
CustomersDataGrid.UpdateLayout();
}
}
}

For more information on multi-page printing, go to the Multipage printing lab at

channel9.msdn.com/learn/courses/Silverlight4/SL4BusinessModule6/SL4LOB_06_Printing_the_Schedule/


Other  
  •  Building LOB Applications : Data Validation through Data Annotation
  •  Building LOB Applications : Implementing CRUD Operations in RIA Services
  •  Microsoft XNA Game Studio 3.0 : Displaying Images - Resources and Content (part 2) - Adding Resources to a Project
  •  Microsoft XNA Game Studio 3.0 : Displaying Images - Resources and Content (part 1)
  •  iPhone 3D Programming : Blending and Augmented Reality - Rendering Anti-Aliased Lines with Textures
  •  Programming with DirectX : Game Math - Bounding Geometry (part 2) - Bounding Spheres & Bounding Hierarchies
  •  Programming with DirectX : Game Math - Bounding Geometry (part 1) - Bounding Boxes
  •  Programming with DirectX : Game Math - Matrices
  •  iPhone 3D Programming : Anti-Aliasing Tricks with Offscreen FBOs (part 2) - Jittering
  •  iPhone 3D Programming : Anti-Aliasing Tricks with Offscreen FBOs (part 1) - A Super Simple Sample App for Supersampling
  •  Building LOB Applications : Navigating RIA LOB Data
  •  Building LOB Applications : Databinding in XAML
  •  Microsoft XNA Game Studio 3.0 : Program Bugs
  •  Microsoft XNA Game Studio 3.0 : Getting Player Input - Adding Vibration
  •  Microsoft XNA Game Studio 3.0 : Getting Player Input - Using the Keyboard
  •  iPhone 3D Programming : Blending and Augmented Reality - Stencil Alternatives for Older iPhones
  •  iPhone 3D Programming : Blending and Augmented Reality - Poor Man’s Reflection with the Stencil Buffer
  •  Microsoft XNA Game Studio 3.0 : Getting Player Input - Reading a Gamepad
  •  iPhone 3D Programming : Blending and Augmented Reality - Shifting Texture Color with Per-Vertex Color
  •  iPhone 3D Programming : Blending and Augmented Reality - Blending Extensions and Their Uses
  •  
    Top 10
    Windows Server 2003 : Domain Name System - Command-Line Utilities
    Microsoft .NET : Design Principles and Patterns - From Principles to Patterns (part 2)
    Microsoft .NET : Design Principles and Patterns - From Principles to Patterns (part 1)
    Brother MFC-J4510DW - An Innovative All-In-One A3 Printer
    Computer Planet I7 Extreme Gaming PC
    All We Need To Know About Green Computing (Part 4)
    All We Need To Know About Green Computing (Part 3)
    All We Need To Know About Green Computing (Part 2)
    All We Need To Know About Green Computing (Part 1)
    Master Black-White Copying
    Most View
    Epic Moments in Sports (Part 1)
    iPhone 3D Programming : Blending and Augmented Reality - Blending Recipe
    HP X2301 : Micro Thin, Macro Sights
    Advanced ASP.NET : Understanding Caching
    Amateur Astronomy Applications (Part 1) - WorldWide Telescope, Stellarium
    How To Buy…SSD Drives (Part 2)
    Macro Marvel by Peiling Lee
    Under The Surface (Part 1)
    Microsoft .NET : Design Principles and Patterns - Object-Oriented Design (part 1) - Basic OOD Principles
    Hosting a Multi-Tenant Application on Windows Azure : Selecting a Single-Tenant or Multi-Tenant Architecture
    Google Nexus 4 - Features Of A High-End Smartphone For Half The Price
    Developing an SEO-Friendly Website: Content Delivery and Search Spider Control (part 2)
    SQL Server 2008 : Explaining Advanced Query Techniques - Managing Internationalization Considerations
    Frequently Asked Questions About UAC
    Windows Server 2008 : Harnessing the Power and Potential of FIM
    Parallel Programming with Microsoft .Net : Dynamic Task Parallelism - An Example
    Microsoft XNA Game Studio 3.0 : Displaying Images - Using Resources in a Game (part 3) - Sprite Drawing with SpriteBatch
    Windows Server 2003 : Active Directory - Understanding Operations Master Roles
    Windows 7 : Using Windows Defender (part 3) - Using Windows Defender Tools & Troubleshooting Windows Defender
    iOS 6 Beta Review (Part 1)