Hi, I am sharing the sample code, please check:
class Program
{
static void Main(string[] args)
{
////Output directory path.
string dirPath = @"C:\Users\brajesh.h.kumar\Desktop\Aspose.Diagram\";
int pageNumber = 0;
// Create a Microsoft Visio document using Aspose.Diagram dll.
Aspose.Diagram.Diagram diagram = new Aspose.Diagram.Diagram(dirPath + "AddConnectShapes.vss");
long rectangleId = diagram.AddShape(0, 0, 0, 0, @"Rectangle", pageNumber);
//long connectorId = diagram.AddShape(0, 0, 0, 0, @"Dynamic connector", pageNumber);
diagram.HeaderFooter.HeaderCenter = "Center of the header";
Aspose.Diagram.Page vsoActivePageAspose = diagram.Pages.GetPage(0);
vsoActivePageAspose.PageSheet.PrintProps.PrintPageOrientation.Value = Aspose.Diagram.PrintPageOrientationValue.Landscape;
diagram.Pages[0].PageSheet.PageProps.PageHeight.Value = 12;
diagram.Pages[0].PageSheet.PageProps.PageWidth.Value = 30;
double X1_AppBox = 0, Y1_AppBox = 0, X2_AppBox = 0, Y2_AppBox = 0;
string AppText = string.Empty, ForeGroundColour = string.Empty;
long rectangleId2 = 0, rectangleId3 = 0,rectangleId1=0, rectangleId4 = 0;
// Create a new diagram
for (int i = 1; i <= 13; i++)
{
if (i == 1)
{ X1_AppBox = 2; Y1_AppBox = 40; X2_AppBox = 3.1; Y2_AppBox = 39.2; AppText = "ServiceNow - EU"; ForeGroundColour = "RGB(225, 168, 164)"; }
else if (i == 2)
{ X1_AppBox = 1.25; Y1_AppBox = 40.75; X2_AppBox = 3.85; Y2_AppBox = 38.45; AppText = "Accenture Hosted\nClient Data Center"; ForeGroundColour = "RGB(224, 224, 224)"; }
else if (i == 3)
{ X1_AppBox = 6.1; Y1_AppBox = 40; X2_AppBox = 7.19; Y2_AppBox = 39.2; AppText = "ServiceNow Connector"; ForeGroundColour = "RGB(112, 48, 160)"; }
else if (i == 4)
{ X1_AppBox = 5.35; Y1_AppBox = 40.75; X2_AppBox = 7.94; Y2_AppBox = 38.45; AppText = "Client Hosted\nAccenture Data Center"; ForeGroundColour = "RGB(224, 224, 224)"; }
else if (i == 5)
{ X1_AppBox = 2; Y1_AppBox = 36.2; X2_AppBox = 3.1; Y2_AppBox = 35.40; AppText = "myWizard ESB"; ForeGroundColour = "RGB(112, 48, 160)"; }
else if (i == 6)
{ X1_AppBox = 6.1; Y1_AppBox = 36.2; X2_AppBox = 7.19; Y2_AppBox = 35.40; AppText = "myWizard Event & Notification Service"; ForeGroundColour = "RGB(112, 48, 160)"; }
else if (i == 7)
{ X1_AppBox = 2; Y1_AppBox = 34.65; X2_AppBox = 3.1; Y2_AppBox = 33.85; AppText = "myWizard Application Management (AM)"; ForeGroundColour = "RGB(176, 193, 212"; }
else if (i == 8)
{ X1_AppBox = 3.85; Y1_AppBox = 34.65; X2_AppBox = 4.95; Y2_AppBox = 33.85; AppText = "myWizard Automation Store"; ForeGroundColour = "RGB(176, 193, 212)"; }
else if (i == 9)
{ X1_AppBox = 5.7; Y1_AppBox = 34.65; X2_AppBox = 6.80; Y2_AppBox = 33.85; AppText = "myWizard Governance"; ForeGroundColour = "RGB(176, 193, 212)"; }
else if (i == 10)
{ X1_AppBox = 7.55; Y1_AppBox = 34.65; X2_AppBox = 8.65; Y2_AppBox = 33.85; AppText = "myWizard Metrics"; ForeGroundColour = "RGB(176, 193, 212)"; }
else if (i == 11)
{ X1_AppBox = 1.25; Y1_AppBox = 36.95; X2_AppBox = 9.4; Y2_AppBox = 33.10; AppText = "myWizard Applications"; ForeGroundColour = "RGB(225, 235, 243)"; }
else if (i == 12)
{ X1_AppBox = 0.5; Y1_AppBox = 37.7; X2_AppBox = 10.15; Y2_AppBox = 32.35; AppText = "myWizard Managed/Shared\nAccenture Data Center"; ForeGroundColour = "RGB(224, 224, 224)"; }
switch (i)
{
case 1:
case 3:
rectangleId1 = diagram.AddShape((X1_AppBox + ((X2_AppBox - X1_AppBox) / 2)), (Y2_AppBox + ((Y1_AppBox - Y2_AppBox) / 2)), X2_AppBox - X1_AppBox, Y1_AppBox - Y2_AppBox, @"Rectangle", pageNumber);
Aspose.Diagram.Shape shape1 = diagram.Pages[pageNumber].Shapes.GetShape(rectangleId1);
shape1.Text.Value.Add(new Aspose.Diagram.Txt(AppText));
shape1.TextBlock.VerticalAlign.Value = Aspose.Diagram.VerticalAlignValue.Middle;
shape1.Name = AppText;
shape1.Fill.FillBkgnd.Value = getHexColorFromRGB(ForeGroundColour);
shape1.Fill.FillForegnd.Value = getHexColorFromRGB(ForeGroundColour);
shape1.SendToBack();
break;
case 2:
case 4:
case 12:
rectangleId4 = diagram.AddShape((X1_AppBox + ((X2_AppBox - X1_AppBox) / 2)), (Y2_AppBox + ((Y1_AppBox - Y2_AppBox) / 2)), X2_AppBox - X1_AppBox, Y1_AppBox - Y2_AppBox, @"Rectangle", pageNumber);
Aspose.Diagram.Shape shape4 = diagram.Pages[pageNumber].Shapes.GetShape(rectangleId4);
shape4.Text.Value.Add(new Aspose.Diagram.Txt(AppText));
shape4.TextBlock.VerticalAlign.Value = Aspose.Diagram.VerticalAlignValue.Top;
shape4.Name = AppText;
shape4.Fill.FillBkgnd.Value = getHexColorFromRGB(ForeGroundColour);
shape4.Fill.FillForegnd.Value = getHexColorFromRGB(ForeGroundColour);
shape4.SendToBack();
break;
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
rectangleId2 = diagram.AddShape((X1_AppBox + ((X2_AppBox - X1_AppBox) / 2)), (Y2_AppBox + ((Y1_AppBox - Y2_AppBox) / 2)), X2_AppBox - X1_AppBox, Y1_AppBox - Y2_AppBox, @"Rectangle", pageNumber);
Aspose.Diagram.Shape shape2 = diagram.Pages[pageNumber].Shapes.GetShape(rectangleId2);
shape2.Text.Value.Add(new Aspose.Diagram.Txt(AppText));
shape2.TextBlock.VerticalAlign.Value = Aspose.Diagram.VerticalAlignValue.Middle;
shape2.Name = AppText;
shape2.Fill.FillBkgnd.Value = getHexColorFromRGB(ForeGroundColour);
shape2.Fill.FillForegnd.Value = getHexColorFromRGB(ForeGroundColour);
shape2.SendToBack();
break;
case 11:
rectangleId3 = diagram.AddShape((X1_AppBox + ((X2_AppBox - X1_AppBox) / 2)), (Y2_AppBox + ((Y1_AppBox - Y2_AppBox) / 2)), X2_AppBox - X1_AppBox, Y1_AppBox - Y2_AppBox, @"Rectangle", pageNumber);
Aspose.Diagram.Shape shape3 = diagram.Pages[pageNumber].Shapes.GetShape(rectangleId3);
shape3.Text.Value.Add(new Aspose.Diagram.Txt(AppText));
shape3.TextBlock.VerticalAlign.Value = Aspose.Diagram.VerticalAlignValue.Top;
shape3.Name = AppText;
shape3.Fill.FillBkgnd.Value = getHexColorFromRGB(ForeGroundColour);
shape3.Fill.FillForegnd.Value = getHexColorFromRGB(ForeGroundColour);
shape3.SendToBack();
break;
default:
break;
}
}
// To Draw the connector
Aspose.Diagram.Shape asposeSourceAppShape=null;
Aspose.Diagram.Shape asposeTargetAppShape=null;
for (int i = 1; i <= 3; i++)
{
asposeSourceAppShape = null;
asposeTargetAppShape = null;
if (i == 1)
{
asposeSourceAppShape = diagram.Pages[pageNumber].Shapes.GetShape("myWizard Automation Store");
asposeTargetAppShape = diagram.Pages[pageNumber].Shapes.GetShape("myWizard Event & Notification Service");
}
else if (i == 2)
{
asposeSourceAppShape = diagram.Pages[pageNumber].Shapes.GetShape("myWizard Event & Notification Service");
asposeTargetAppShape = diagram.Pages[pageNumber].Shapes.GetShape("myWizard ESB");
}
else if (i == 3)
{
asposeSourceAppShape = diagram.Pages[pageNumber].Shapes.GetShape("myWizard ESB");
asposeTargetAppShape = diagram.Pages[pageNumber].Shapes.GetShape("ServiceNow Connector");
}
// Add shape for Line
Aspose.Diagram.Shape shapeLine = new Aspose.Diagram.Shape();
shapeLine.Line.EndArrow.Value = 3;
shapeLine.Line.LineWeight.Value = 0.01388;
shapeLine.Line.Rounding.Value = 4;
shapeLine.Line.LineColor.Value = getHexColorFromRGB("RGB(192, 0, 0)");
long connecter1Id = diagram.AddShape(shapeLine, @"Dynamic connector", diagram.Pages[0].ID);
diagram.Pages[pageNumber].ConnectShapesViaConnector(asposeSourceAppShape.ID, Aspose.Diagram.Manipulation.ConnectionPointPlace.Top,
asposeTargetAppShape.ID, Aspose.Diagram.Manipulation.ConnectionPointPlace.Bottom, connecter1Id);
}
DiagramSaveOptions options = new DiagramSaveOptions(Aspose.Diagram.SaveFileFormat.VSDX);
// Set Auto fit page property
options.AutoFitPageToDrawingContent = true;
// Save the diagram in Microsoft Visio VSDX format.
diagram.Save(dirPath + Guid.NewGuid() + ".vsdx", options);
}
static string getHexColorFromRGB(string rgbcolor)
{
var splitRGB = rgbcolor.Replace("(", string.Empty).Replace(")", string.Empty).Replace("RGB", string.Empty).Split(',');
if (splitRGB.Length == 3)
{
Color myColor = Color.FromArgb(System.Convert.ToInt32(splitRGB[0]), System.Convert.ToInt32(splitRGB[1]), System.Convert.ToInt32(splitRGB[2]));
string hex = myColor.R.ToString("X2") + myColor.G.ToString("X2") + myColor.B.ToString("X2");
return string.Format("#{0}", hex);
}
return string.Empty;
}
}