Printing in Silverlight: Printing Charts and Auto Scaling

Printing support has been introduced in Silverlight 4. This means that any part of the visual tree can be printed with some simple API calls (see the Printing Basics section on my Advanced Printing in Silverlight post). This post provides an example to print charts and looks at how elements can be auto scaled to fit the page size.

Printing a chart

There are lots of examples on blogs demonstrating printing on simple elements. However I wanted to test how the printing functionality performs when working with charts.

I chose to use the most widespread (and free) charting component, the Silverlight Toolkit. In my example I’ve created four charts and added printing support to all of them like this:

Chart chartToPrint; // The element to be printed
PrintDocument doc = new PrintDocument(); // Create the PrintDocument  object that will do the printing
doc.PrintPage += (s, args) =>
{
  // Set the chart that needs to be printed.
  // As soon as this is set, printing starts
  args.PageVisual = chartToPrint;
}

In the example the whole page can be printed as well by clicking on the Print whole page button. In this case the LayoutRoot element is passed as the PageVisual. Test the example yourself:

Auto scale the charts to fit the page

Printing worked good, however when printing a page the chart kept it’s original size which was only fraction of the page. I was looking for a way to increase the size of the printed chart to fill out the printed page.

The solution consisted of two steps. First, the Width and Height of the chart had to be set to the PrintableArea.Width and PrintableArea.Height value within the PrintPage event of the PrintDocument. Second, the effects of changing the chart size don’t take effect immediately and I only want to start printing once the chart has been resized. So I’ve registered the SizeChanged event of the chart to update the PageVisual of the printed page once the chart has been resized.

doc.PrintPage += (s, args) =>
{
    elementToPrint.Width = args.PrintableArea.Width;
    elementToPrint.Height= args.PrintableArea.Height;
    // Printing only starts when args.PageVisual != null
    // Only set args.PageVisual to elementToPrint once its size has been updated
    elementToPrint.SizeChanged += (s1, args1) =>
    {
       args.PageVisual = elementToPrint;
     };
}

(For a bit more detail on this step see the Auto scaling charts to fit the page section here)

I’ve created a new example that now supports printing without changing size and printing that auto scales the charts to match page size:

The source of this application can be downloaded from here: PrintingExample.zip.

For more detailed information on printing events and some more thoughts on auto scaling and multi page printing read my blog post Advanced Printing in Silverlight: Printing Charts and Auto Scaling on my Scott Logic blog.

5 Responses to Printing in Silverlight: Printing Charts and Auto Scaling
  1. Trackback: DotNetShoutout
    Advanced Printing in Silverlight... Thank you for submitting this cool story - Trackback fro... dotnetshoutout.com/Advanced-Printing-in-Silverlight
  2. Trackback: Auto tips
    Auto tips... [...]Greg Does IT » Blog Archive » Printing in Silverlight: Printin... gospelofbiz.com/auto-tips/automotive-marketing-research-redefining-a-new-level
  3. Trackback: Memjet
    Memjet... [...]Greg Does IT » Blog Archive » Printing in Silverlight: Printing C... kwiker.co.uk/product-types/bundles
  4. [...] Greg Does IT » Blog Archive » Printing in Silverlight: Printing Charts …May 7, 2010 R... rentoblog.shikshik.org/2012/03/12/printing-charts