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/