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
- Create MS Visio Diagram using C#
- Add Master to VSDX Diagram in C#
- Insert Pages in a Visio Diagram in C#
- Create a Shape in a Visio Diagram in C#
- Add a Text Shape in the Visio Page in C#
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:
- Create an instance of Diagram class.
- Use the Diagram.Save(Sring fileName, SaveFileFormat.VSDX) method to save the file as VSDX.
The following code sample shows how to create Visio VSDX diagram in C#.
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.
- Create a new diagram or load an existing one using the Diagram class.
- Add master using Diagram.AddMaster(String fileName, Int masterID) method.
- Add a shape from the master to the diagram using the Diagram.AddShape(Double, Double, String, Int) method.
- Save diagram using Diagram.Save(Sring fileName, SaveFileFormat.VSDX) method.
The following code sample shows how to add a master to the Visio diagram using C#.
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.
- Create a new diagram or load an existing one using the Diagram class.
- Check if the diagram already contains a page using Diagram.Pages.Count property.
- If it does, get the ID of the last page using Diagram.Pages[index].ID property.
- Create a new page using Page class and set its name and ID.
- Add page to the diagram using the Diagram.Pages.Add(Page) method.
- Save VSDX diagram using Diagram.Save(Sring fileName, SaveFileFormat.VSDX) method.
The following code sample shows how to add a page in Visio VSDX diagram using C#.
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.
- Create a new diagram or load an existing one using the Diagram class.
- Create a Page or get the desired page in a Page object.
- Add master to the diagram using Diagram.AddMaster(String fileName, Int masterID) method.
- Add a new rectangular shape using Diagram.AddShape(pinX, pinY, width, height, masterName, PageIndex) method.
- Store the shape ID returned by Diagram.AddShape() method.
- Retrieve the newly added shape in the Shape object using Page.Shapes.GetShape(long ID) method.
- Set shape’s properties such as text, color, etc.
- Save VSDX diagram using Diagram.Save(Sring fileName, SaveFileFormat.VSDX) method.
The following code sample shows how to add a shape in the Visio diagram using C#.
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.
- Create a new diagram or load an existing one using the Diagram class.
- Add text to a particular page using Diagram.Pages[0].AddText(PinX, PinY, Width, Height, “Test text”) method.
- Save the VSDX diagram using Diagram.Save(Sring fileName, SaveFileFormat.VSDX) method.
The following code sample shows how to add text in a VSDX diagram using C#.
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.