How to Print a Graph with Silverlight 4

Tips & Tricks

Summary

This article shows how the contents of a CanvasControl (e.g. a graph) can be printed in yFiles for Silverlight together with Silverlight 4.

This article was written for an older version. It is only online as a reference for customers using this old version. The information it contains is probably out of date.

The latest information can be found in the yFiles documentation

Description

Since Silverlight 4, printing is supported by the Silverlight platform. yFiles for Silverlight versions before 2.0 that target the Silverlight 3 environment, offer no printing support out of the box. However, it is easily possible to use the printing support in Silverlight 4 together with these versions of yFiles for Silverlight.

yFiles for Silverlight 2.0 and higher support printing out of the box. Please refer to the yFiles for Silverlight Developer's Guide for further information.

The following example code (yFiles for Silverlight versions before 2.0) prints the current contents of the graph control.
This code requires Silverlight 4.
This code must be used in response to a button press or other user interaction, because Silverlight requires user interaction to open up a print dialog.
ContextConfigurator cc = new ContextConfigurator(graphControl.Viewport, graphControl.Zoom);

Transform transform;
Geometry clip;
IRenderContext renderContext = cc.Setup(graphControl, out transform, out clip);
FrameworkElement content = graphControl.CreateVisualContent(renderContext);

double w = graphControl.GetContentHost().ActualWidth;
double h = graphControl.GetContentHost().ActualHeight;

content.Measure(new Size(w, h));
content.Arrange(new Rect(0, 0, w, h));

PrintDocument pd = new PrintDocument();
pd.PrintPage += delegate(object o, PrintPageEventArgs args) {
                   args.PageVisual = new CanvasContainer()
                     {Children = {content}, RenderTransform = transform};
                   args.HasMorePages = false;
                };
pd.Print("yFiles Print Document");

Categories this article belongs to:
yFiles for Silverlight > yFiles for Silverlight Viewer > Input and Output
Applies to:
yFiles for Silverlight: 1.0, 1.0.1
Keywords:
printer - printing - PrintDocument - ContextConfigurator - Silverlight 4