Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active March 16, 2022 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/0c3a80973adbc7e9a946e08e0e77febf to your computer and use it in GitHub Desktop.
Save aspose-com-gists/0c3a80973adbc7e9a946e08e0e77febf to your computer and use it in GitHub Desktop.
Create Flowchart Visio Diagram Programmatically in C# .NET
class Program
{
static void Main(string[] args)
{
// schema for the diagram to be created
Input diagramObject = new Input()
{
InputRectangles = new List<InputRectangle>() {
new InputRectangle() {
Name = "A",
Text = "Manager"
},
new InputRectangle() {
Name = "B",
Text = "Team Leader"
},
new InputRectangle() {
Name = "C",
Text = "Team Member"
},
new InputRectangle() {
Name = "D",
Text = "Team Member"
},
new InputRectangle() {
Name = "E",
Text = "Team Member"
}
},
InputConnectors = new List<InputConnector>() {
new InputConnector() {
OriginShapeName = "A",
DestinationShapeName = "B"
},
new InputConnector() {
OriginShapeName = "B",
DestinationShapeName = "C"
},
new InputConnector() {
OriginShapeName = "B",
DestinationShapeName = "D"
},
new InputConnector() {
OriginShapeName = "B",
DestinationShapeName = "E"
}
}
};
Diagram diagram = new Diagram("BasicShapes.vss");
Page page = diagram.Pages[0];
Dictionary<string, long> shapeNames = new Dictionary<string, long>();
//Adding shapes and connectors from the schema
foreach (var rectangle in diagramObject.InputRectangles)
{
Shape shape = new Shape();
var shapeId = diagram.AddShape(shape, @"Rectangle", 0);
shapeNames.Add(rectangle.Name, shapeId);
shape = page.Shapes.GetShape(shapeId);
shape.Text.Value.Add(new Txt(rectangle.Text));
}
foreach (var connector in diagramObject.InputConnectors)
{
var connectorId = diagram.AddShape(new Shape(), "Dynamic connector", 0);
page.ConnectShapesViaConnector(shapeNames[connector.OriginShapeName],
ConnectionPointPlace.Right,
shapeNames[connector.DestinationShapeName],
ConnectionPointPlace.Left,
connectorId);
}
LayoutOptions layoutOptions = new LayoutOptions()
{
LayoutStyle = LayoutStyle.FlowChart,
Direction = LayoutDirection.LeftToRight,
SpaceShapes = 5,
EnlargePage = true
};
diagram.Layout(layoutOptions);
page.PageSheet.PrintProps.PrintPageOrientation.Value = PrintPageOrientationValue.Landscape;
DiagramSaveOptions saveOptions = new DiagramSaveOptions()
{
SaveFormat = SaveFileFormat.Vsdx,
AutoFitPageToDrawingContent = true
};
diagram.Save("output.vsdx", saveOptions);
}
}
public class Input
{
public System.Collections.Generic.List<InputRectangle> InputRectangles { get; set; }
public System.Collections.Generic.List<InputConnector> InputConnectors { get; set; }
}
public class InputRectangle
{
public string Name { get; set; }
public string Text { get; set; }
}
public class InputConnector
{
public string OriginShapeName { get; set; }
public string DestinationShapeName { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment