Document Solutions for Excel, .NET Edition | Document Solutions
Features / Shapes and Pictures / Group or Ungroup Shapes
In This Topic
    Group or Ungroup Shapes
    In This Topic

    DsExcel allows you to group or ungroup shapes in a worksheet. Shapes can be grouped together when there is a need to perform certain action on the bunch of shapes together. For example: adding similar style to shapes, aligning, rotating, copying or pasting the grouped shapes together. It does not only saves a considerable amount of time and efforts but also helps in ensuring that the desired consistency is maintained in all the shapes.

    Group Shapes

    Several shapes can be grouped together using the Group method of the IShapeRange interface. The IShapeRange interface represents the range of the shapes which needs to be grouped together. The grouped shapes behave as a single shape.

    Refer to the following example code to group shapes.

    Copy Code
    // Initialize workbook
    Workbook workbook = new Workbook();
    // Fetch default worksheet 
    IWorksheet worksheet = workbook.Worksheets[0];
    //Creating shapes collection for activeSheet
    IShapes shapes = workbook.ActiveSheet.Shapes;
    // Adding Shapes to shapes collection
    IShape ShapeBegin = shapes.AddShape(AutoShapeType.Wave, 10, 10, 100, 100);
    IShape EndBegin = shapes.AddShape(AutoShapeType.RoundedRectangle, 200, 200, 100, 100);
    // Adding Connector Shape to shapes collection
    IShape ConnectorShape = shapes.AddConnector(ConnectorType.Straight, 10, 10, 101, 101);
    //Connecting ShapeBegin & EndBegin shapes by connector shape
    ConnectorShape.ConnectorFormat.BeginConnect(ShapeBegin, 3);
    ConnectorShape.ConnectorFormat.EndConnect(EndBegin, 0);
    //Adding IsoscelesTriangle shape to shapes collection
    shapes.AddShape(AutoShapeType.IsoscelesTriangle, 370.8, 50.8, 81.6, 102.0);
    //Creating shpRange collection to group certain shapes as given in array
    IShapeRange shpRange = shapes.Range[new string[3] { shapes[0].Name, shapes[1].Name, shapes[2].Name }];
    // Grouping Shapes
    IShape grouped = shpRange.Group();
    // Setting Style for Grouped shape together
    grouped.Line.Color.RGB = System.Drawing.Color.DarkOrange;
    grouped.Fill.Color.RGB = System.Drawing.Color.LightGreen;
    Console.WriteLine("Group Name is: " + grouped.Name);
    // Saving workbook to Xlsx
    workbook.Save(@"GroupedShapes.xlsx", SaveFileFormat.Xlsx);

    Ungroup Shapes

    A group of shapes in a specified range can be ungrouped using the Ungroup method of the IShape interface.

    Refer to the following example code to ungroup shapes.

    Copy Code
    // Initialize workbook
    Workbook workbook = new Workbook();
    // Open workbook
    IShapes shapes = workbook.Worksheets[0].Shapes;
    // UnGroup Shapes
    for (int i = 0; i < shapes.Count; i++)
        if (shapes[i].Type == ShapeType.Group) // Or,  if (shapes[i].Name == "Group 1")
    // Or, we can just pass GroupName to Ungroup it
    // shapes["Group 1"].Ungroup();
    // Saving workbook to Xlsx
    workbook.Save(@"10-UnGroupedShapes.xlsx", SaveFileFormat.Xlsx);