Printing the Diagram

To print a diagram, use the DiagramPrinter class. This provides you with a DiagramPrintDocument which in turn yields a WPF FixedDocument which can be passed to an XpsDocumentWriter.

You’ll need to reference the System.Printing and ReachFramework DLLs to get printing support for your application.
 

Here’s the basic code to print a diagram using a user-selected printer:

CopyC#
PrintDocumentImageableArea imageableArea = null;
// This prompts the user to select a printer
XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter("demo", ref imageableArea);
if (writer != null)
{
  DiagramPrinter printer = new DiagramPrinter
  {
    Diagram = ds.Diagram,
    Formatter = ds.Formatter,
    Zoom = 1.5
  };

  DiagramPrintDocument doc = printer.GetPrintDocument(imageableArea);

  writer.Write(doc.Document);
}

The DiagramPrinter must be supplied with a diagram and a formatter just as a DiagramSurface must be. In this case, we just reference the diagram and formatter on the DiagramSurface, because we want the printout to look like the display, but we could have used a different formatter specialised for printing, or we could print a diagram without ever displaying it at all.

What if you want to share diagrams electronically, for example via email or the Web? One option is to print to an XPS file, but for most applications it’s easier to export to a bitmap file. We’ll see how to do that next.

Next: Exporting to a Bitmap.