MS Visio is a popular application that lets you create a wide range of diagrams such as flowcharts, data flow diagrams, business process models and etc. VSDX is the file format that MS Visio uses to store the diagrams. In order to automate VSDX manipulation, this article provides you a basic tutorial of how to create Visio diagrams from scratch in C#. Furthermore, it covers how to insert pages, shapes, and text in the VSDX diagrams from within .NET applications.

C# Visio API - Free Download

Aspose.Diagram for .NET is a feature-rich API that lets you create, edit, convert, and process MS Visio diagrams from within your .NET applications. The API makes it easier for you to manipulate the VSDX diagrams with easy to use properties and methods. You can either download API’s DLL or get it installed within your .NET applications using NuGet.

Install-Package Aspose.Diagram

Create Visio VSDX Diagram using C#

First of all, let’s create an empty VSDX diagram from scratch. The following are the steps to do so:

The following code sample shows how to create Visio VSDX diagram in C#.

// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Diagrams();
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Initialize a new Visio
Diagram diagram = new Diagram();
dataDir = dataDir + "CreateDiagram_out.vsdx";
// Save in the VSDX format
diagram.Save(dataDir, SaveFileFormat.VSDX);

Add Master to Visio Diagram in C#

Master is used to add the stencil which contains a collection of shapes to be used in diagrams. If you want to add the master, you need a VSS stencil file and master ID. The following are the steps to add a master to the Visio diagram using Aspose.Diagram.

The following code sample shows how to add a master to the Visio diagram using C#.

// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Master();
// Load diagram
Diagram diagram = new Diagram();
// Load stencil to a stream
string templateFileName = dataDir + "NetApp-FAS-series.vss";
Stream stream = new FileStream(templateFileName, FileMode.Open);
// Add master with stencil file path and master id
string masterName = "FAS80xx rear empty";
diagram.AddMaster(templateFileName, 2);
// Add master with stencil file path and master name
diagram.AddMaster(templateFileName, masterName);
// Add master with stencil file stream and master id
diagram.AddMaster(stream, 2);
// Adds master to diagram from source diagram
Diagram src = new Diagram(templateFileName);
diagram.AddMaster(src, masterName);
// Add master with stencil file stream and master id
diagram.AddMaster(stream, masterName);
// Adds shape with defined PinX and PinY.
diagram.AddShape(2.0, 2.0, masterName, 0);
diagram.AddShape(6.0, 6.0, masterName, 0);
// Adds shape with defined PinX,PinY,Width and Height.
diagram.AddShape(7.0, 3.0, 1.5, 1.5, masterName, 0);

For more details, please visit working with master.

Insert Pages in a Visio Diagram in C#

MS Visio diagrams consist of one or more pages and each page contains the diagrams. Therefore, before adding a shape, you need to add a page using the following steps.

The following code sample shows how to add a page in Visio VSDX diagram using C#.

// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_VisioPages();
// Load diagram
Diagram diagram = new Diagram(dataDir + "Drawing1.vsdx");
// It calculates max page id
int max = 0;
if (diagram.Pages.Count != 0)
max = diagram.Pages[0].ID;
for (int i = 1; i < diagram.Pages.Count; i++)
{
if (max < diagram.Pages[i].ID)
max = diagram.Pages[i].ID;
}
// Set max page ID
int MaxPageId = max;
// Initialize a new page object
Page newPage = new Page();
// Set name
newPage.Name = "new page";
// Set page ID
newPage.ID = MaxPageId + 1;
// Or try the Page constructor
// Page newPage = new Page(MaxPageId + 1);
// Add a new blank page
diagram.Pages.Add(newPage);
// Save diagram
diagram.Save(dataDir + "InsertBlankPage_out.vsdx", SaveFileFormat.VSDX);

For more details, please visit working with pages.

Create a Shape in Visio Diagram using C#

Shapes are the building blocks of the Visio diagrams. MS Visio supports a wide range of shapes to create diagrams in various domains. The following steps show how to insert a shape in Visio Diagram.

The following code sample shows how to add a shape in the Visio diagram using C#.

// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Shapes();
// Load a diagram
Diagram diagram = new Diagram(dataDir + "Drawing1.vsdx");
// Get page by name
Page page = diagram.Pages.GetPage("Page-2");
// Add master with stencil file path and master name
string masterName = "Rectangle";
diagram.AddMaster(dataDir + "Basic Shapes.vss", masterName);
// Page indexing starts from 0
int PageIndex = 1;
double width = 2, height = 2, pinX = 4.25, pinY = 4.5;
// Add a new rectangle shape
long rectangleId = diagram.AddShape(pinX, pinY, width, height, masterName, PageIndex);
// Set shape properties
Shape rectangle = page.Shapes.GetShape(rectangleId);
rectangle.XForm.PinX.Value = 5;
rectangle.XForm.PinY.Value = 5;
rectangle.Type = TypeValue.Shape;
rectangle.Text.Value.Add(new Txt("Aspose Diagram"));
rectangle.TextStyle = diagram.StyleSheets[3];
rectangle.Line.LineColor.Value = "#ff0000";
rectangle.Line.LineWeight.Value = 0.03;
rectangle.Line.Rounding.Value = 0.1;
rectangle.Fill.FillBkgnd.Value = "#ff00ff";
rectangle.Fill.FillForegnd.Value = "#ebf8df";
diagram.Save(dataDir + "AddShape_out.vsdx", SaveFileFormat.VSDX);
Console.WriteLine("Shape has been added.");

For more details, please visit working with shapes.

Add a Text Shape in the Visio Page in C#

In various cases, you also need to add text to the Visio diagrams. For this, you can follow the below steps.

The following code sample shows how to add text in a VSDX diagram using C#.

// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_ShapeText();
// Create a new diagram
Diagram diagram = new Diagram();
// Set parameters and add text to a Visio page
double PinX = 1, PinY = 1, Width = 1, Height = 1;
diagram.Pages[0].AddText(PinX, PinY, Width, Height, "Test text");
// Save diagram
diagram.Save(dataDir + "InsertTextShape_out.vsdx", SaveFileFormat.VSDX);

For more details, please visit working with text.

Conclusion

In this post, you have learned some basic features of Aspose.Diagram for .NET to create Visio VSDX diagrams from scratch. The code samples have shown how to add masters, pages, shapes, and text in VSDX diagrams using C#. You can explore more about the API using the documentation.

See Also