Printing a Canvas's Content

yFiles for Silverlight offers various possibilities for printing the contents of a CanvasControl. Readily available with class CanvasControl, for example, are the following methods:

void Print()
void Print(RectD worldRect, InsetsD? pageMargins)
Description CanvasControl's printing methods. They enable both instant printing mostly using the canvas's current settings or using custom settings.

Ultimately, both these methods use class CanvasPrintDocument for the actual printing. CanvasPrintDocument provides all necessary functionality for printing the contents of a canvas. It supports scaling and clipping options, and also enables poster printing. Figure 4.40, “CanvasPrintDocument” gives an overview on the hierarchy of printing classes.

Figure 4.40. CanvasPrintDocument


The CanvasControl whose content is to be printed is given either at creation time of the instance or using the Canvas property.

Specifying the part of the canvas that should be printed is done in world coordinates using the PrintRectangle property. Unless a clipping is explicitly provided that way, the entire contents of the canvas, i.e., the rectangle as returned by the CanvasControl's ContentRect property, is printed. Example 4.49, “Printing a canvas's currently visible contents” shows how to print the currently visible contents of a control.

Example 4.49. Printing a canvas's currently visible contents

public void PrintVisible(CanvasControl control) {
  // Print the currently visible contents of the given control.
  CanvasPrintDocument cpd = new CanvasPrintDocument(control);
  cpd.PrintRectangle = control.Viewport;
  cpd.Print(true, true);

Scaling and Poster Printing

CanvasPrintDocument allows to apply a scaling factor when printing the rectangle that is specified with the PrintRectangle property. This factor can be conveniently set by means of the Scale property.

Initially, however, CanvasPrintDocument prints only one page regardless of the actual dimensions of the scaled print rectangle. Due to the ScaleDownToFitPage property, which is true by default, poster printing is effectively disabled.

Example 4.50, “Enabling poster printing” shows how to switch CanvasPrintDocument from single-page mode to poster printing and how to specify a scaling factor to be applied when the visible contents of a control are printed.

Example 4.50. Enabling poster printing

// 'control' is of type yWorks.Canvas.CanvasControl.

CanvasPrintDocument cpd = new CanvasPrintDocument(control);
// Effectively disables single-page mode.
cpd.ScaleDownToFitPage = false;
cpd.PrintRectangle = control.Viewport;
cpd.Scale = 2.0;
cpd.Print(true, true);

The number of columns and rows for poster printing is automatically derived from the actual dimensions of the scaled print rectangle during the printing process. The following method can be used to query the values for column and row count:

Size CalculatePosterSize(SizeD size, InsetsD margins)
Description Calculates the number of columns and rows for the scaled print rectangle.