Blogs

The Progress Bar - DevExpress XPF Blog

Silverlight Grid Control - Printing and Exporting

     

In my previous blog post, I provided an introduction to binding the grid, applying themes, and displaying the print preview embedded in the Silverlight Window Control. For the sake of simplicity, I left out details on how to print and export the grid’s data. That content was reserved for this post and so I am going to take the print preview code I posted yesterday and explain what it actually does. The last part of the post will include directions on how to implement a report service to provide printing and export functionality.

So let’s take a look at the code...

  1. private void printGrid(){
  2.     DocumentPreview preview = new DocumentPreview();
  3.     VisualDataNodeLink link = new VisualDataNodeLink(gridControl1.View as IRootDataNodeSource);
  4.     link.ExportServiceUri = "../ExportService1.svc";
  5.     link.PrintingSystem.ExportOptions.Html.EmbedImagesInHTML = true;
  6.     LinkPreviewModel model = new LinkPreviewModel(link);
  7.     preview.Model = model;
  8.  
  9.     // Setup the DXWindow and content
  10.     printPreviewWindow.Title = "Grid Print Preview";
  11.     printPreviewWindow.Show();
  12.     printPreviewWindow.Content = preview;
  13.  
  14.     link.CreateDocument(true);
  15. }

If you recall, this is the method from my earlier example that when called will generate the print preview and embed the preview control in a DXWindow called “printPreviewWindow”. Here, first a DocumentPreview instance is created - this is the print preview control that will generate and display the preview at runtime. Next, a VisualDataNodeLink is created and the grid’s view is passed on as an IRootDataNodeSource interface.

The link’s ExportServiceUri property specifies the Silverlight-enabled Export Service to be used when printing the data or exporting to an external file format. If exporting the file as an HTML, the PrintingSystem.ExportOptions.Html.EmbedImagesInHTML property specifies whether or not images (if any) should be embedded in the generated HTML file. While I’ve used HTML as an example here, remember that this applies to all the export formats and can be accessed through the PrintingSystem.ExportOptions property collection. So for example, to programmatically set the PDF export image quality, I would use something like this:

  1. link.PrintingSystem.ExportOptions.Pdf.ImageQuality = DevExpress.XtraPrinting.PdfJpegImageQuality.Highest;

Next, the preview model is created and assigned to the document preview. The three lines that follow set up the DXWindow control (specified independently in XAML) and display the print preview control.

  1. // Setup the DXWindow and content
  2.     printPreviewWindow.Title = "Grid Print Preview";
  3.     printPreviewWindow.Show();
  4.     printPreviewWindow.Content = preview;

Finally, the last line generates the document preview:

  1. link.CreateDocument(true);

If I run the application as is and try to export it to a PDF, I’ll get the following error:

Print_Preview_Export_Error

I’m seeing this error message because the application is trying to query an export service (in this case, “ExportService1.svc”) that doesn’t exist. So let’s go ahead and add that to our project.

I’ll right-click the web application project and select to add a new item.

Project_Add_New_Item

From the “Add New Item” dialog, I’ll select the “Silverlight” template category, then the “Silverlight-enabled Export Service” from DevExpress.

DevExpress_Silverlight_Export_Service

Make sure the name is the same as the one used in code earlier. Now, when I run the application, I can print as well as export the data as displayed in the print preview control at runtime.

The complete project for this solution can be found attached to the previous post: Silverlight Grid, Print Preview, and the Silverlight Window Control

Published Jan 03 2011, 03:55 PM by Emil Mesropian (DevExpress)
Bookmark and Share

Comments

No Comments
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.