Search
Exporting the Diagram

Exporting Images

You can create a static image representing the current diagram using the CreateImage method. The method returns an instance of the BitmapSource class. The following example shows how to export a diagram to a Png image file in response to a menu item click, using the SaveFileDialog control where the location is selected and the name of the image file is entered:

C#  Copy Code

private void OnExportImage(object sender, RoutedEventArgs e)
{
    // Configure save file dialog box
    var dlg = new SaveFileDialog
    {
        FileName = "Diagram",
        DefaultExt = ".png",
        Filter = "Diagram Files (.png)|*.png"
    };

    // Show save file dialog box
    var result = dlg.ShowDialog();

    // Process save file dialog box results
    if (result == true)
    {
        var encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(diagram.CreateImage(100)));

        using (var stream = new FileStream(dlg.FileName, FileMode.Create))
            encoder.Save(stream);
    }
}

The MindFusion.Diagramming.Wpf.Export Namespace contains classes for exporting to Dxf, Pdf, Svg and Visio formats.

Exporting to PDF

Add references to the MindFusion.Diagramming.Wpf.PdfExport.dll and MindFusion.Pdf.dll assemblies to your project. Create a PdfExporter instance and call its Export method. Margins specifies the page margins for all pages in the document. If you need to set the page size and orientation, use the PageSize, and PageOrientation properties. The AutoScale property allows scaling the diagram to fit in one or more PDF pages. If invisible items should be displayed in the PDF, enable the InvisibleItems property. The following example shows how to export a diagram to a Pdf file in response to pressing a button event and using the SaveFileDialog control where the location is selected and the name of the Pdf file is entered:

C#  Copy Code

private void OnExportPdf(object sender, RoutedEventArgs e)
{
    // Configure save file dialog box
    var dlg = new SaveFileDialog
    {
        FileName = "Diagram",
        DefaultExt = ".pdf",
        Filter = "Diagram Files (.pdf)|*.pdf"
    };

    // Show save file dialog box
    var result = dlg.ShowDialog();

    // Process save file dialog box results
    if (result == true)
    {
        PdfExporter pdfex = new PdfExporter();
        pdfex.PageSize = MindFusion.Pdf.PageSize.DiagramSize;
        pdfex.PageOrientation = MindFusion.Pdf.PageOrientation.Portrait;
        pdfex.Margins = new Thickness(48, 48, 48, 48);

        pdfex.Export(diagram, dlg.FileName);
    }
}

Exporting to SVG

Add references to the MindFusion.Diagramming.Wpf.SvgExport.dll and MindFusion.Svg.dll assemblies to your project. Create an SvgExporter instance and call its Export method, passing a Diagram object and the full path to the .svg file as parameters. To export images displayed by diagram items to external files, enable the ExternalImages property.

C#  Copy Code

var svgExp = new SvgExporter();
svgExp.ExternalImages = false;
svgExp.Export(diagram, dlg.FileName);

Exporting WMF images

Add a reference to the MindFusion.Diagramming.Wpf.WmfExport.dll assembly to your project. Create a WmfExporter instance and call its Export method, passing a Diagram object and the full path to the .wmf file as parameters.

Exporting Visio drawings

In order to enable Visio export, add a reference to the MindFusion.Diagramming.Wpf.VisioExport.dll to your project. Create a new instance of the VisioExporter class and call its Export method passing a Diagram object and the full path to the exported Visio .vdx file as parameters. The Visio page dimension and items size measure unit is set to the diagram's MeasureUnit.

ShapeNode objects are mapped to their closest Visio counterparts and TableNode objects are exported as groups of Visio shapes. DiagramLink instances are exported as Visio connectors of a similar style. In addition, most of the attributes of diagram items such as text, text formatting, fill and line colors and styles are preserved.

The exporter object requires the VisioExport.vxt file to be present in the application's directory. This file contains an XML template data used by the component to generate Visio documents.

To create Visio groups from the WpfDiagram groups, enable the CreateVisioGroups property. If a shape node that contains an image should be exported as a Visio group of a shape, picture and text objects, use the ExportImagesAsGroups property set to true by default. ExportInvisibleItems allows exporting of invisible items. If TableNode objects should be exported as groups of Visio rectangular shapes, enable the ExportTablesAsGroups property.

The following example shows how to export a diagram to a Visio file in response to choosing a File > Export to > Visio menu item and using the SaveFileDialog control where the location is selected and the name of the Visio file is entered:

C#  Copy Code

private void OnExportVisio(object sender, RoutedEventArgs e)
{
    var dlg = new SaveFileDialog();
    dlg.Filter = "Visio files (*.vdx)|*.vdx|All files|*.*||";

    if (dlg.ShowDialog(this) == true)
    {
        var visioExporter = new VisioExporter();
        visioExporter.Export(diagram, dlg.FileName);
    }
}

Export Excel files

The ExcelExporter class from MindFusion.Diagramming.Wpf.ExcelExport.dll assembly exports diagrams to Excel Open XML Format (XLSX) files. Diagrams are exported as Excel AutoShapes drawings. The Export overload that takes a DiagramDocument parameter creates a sheet for each DiagramPage in the document. The Export(Diagram) overload creates a single Excel sheet.

Export XPS files

The XpsExporter class from MindFusion.Diagramming.Wpf.XpsExport.dll assembly exports diagrams to XPS (XML Paper Specification) files. The Export method overloads let you export either Diagram or DiagramDocument objects.

Export DXF files

Add a reference to the MindFusion.Diagramming.Wpf.DxfExport.dll to your project. Create a DxfExporter instance and call its Export method, specifying a Diagram object and a file path as arguments. Raster images can be used in DXF by referencing external image files. To export the images displayed in diagram elements, enable the ExportImages property. If your target application supports the multiple-line text DXF entity - MTEXT, use the ExportTextAsMultiline property. The following example shows how to create a DxfExporter instance and export a diagram by performing the Export method passing the Diagram object and the file name entered in a SaveFileDialog.

C#  Copy Code

var dxfExporter = new MindFusion.Diagramming.Wpf.Export.DxfExporter();
dxfExporter.Export(diagram, dlg.FileName);