How to Group Shapes in PowerPoint in C#, VB.NET

You can group shapes so that they function as a unit. It's useful to group shapes if you regularly use them together. In this article, you will learn how to create grouped shapes in a PowerPoint slide, by using Spire.Presentation with C# and VB.NET.

Step 1: Create a PowerPoint document and get the first the slide.

Presentation ppt = new Presentation();
ISlide slide= ppt.Slides[0];

Step 2: Create two shapes in the slide.

IShape rectangle = slide.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(20, 100, 200, 40));
rectangle.Fill.FillType = FillFormatType.Solid;
rectangle.Fill.SolidColor.KnownColor = KnownColors.Gold;
rectangle.Line.Width = 0.1f;
IShape ribbon = slide.Shapes.AppendShape(ShapeType.Ribbon2, new RectangleF(60, 75, 120, 80));
ribbon.Fill.FillType = FillFormatType.Solid;
ribbon.Fill.SolidColor.KnownColor = KnownColors.Purple;
ribbon.Line.Width = 0.1f;

Step 3: Add the two shape objects to an array list.

ArrayList list = new ArrayList();
list.Add(rectangle);
list.Add(ribbon);

Step 4: Grope the shapes in the list.

ppt.Slides[0].GroupShapes(list);

Step 5: Save to file.

ppt.SaveToFile("output.pptx", FileFormat.Pptx2010);

Output:

How to Group Shapes in PowerPoint in C#, VB.NET

Full Code:

[C#]
Presentation ppt = new Presentation();
ISlide slide= ppt.Slides[0];

IShape rectangle = slide.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(20, 100, 200, 40));
rectangle.Fill.FillType = FillFormatType.Solid;
rectangle.Fill.SolidColor.KnownColor = KnownColors.Gold;
rectangle.Line.Width = 0.1f;
IShape ribbon = slide.Shapes.AppendShape(ShapeType.Ribbon2, new RectangleF(60, 75, 120, 80));
ribbon.Fill.FillType = FillFormatType.Solid;
ribbon.Fill.SolidColor.KnownColor = KnownColors.Purple;
ribbon.Line.Width = 0.1f;

ArrayList list = new ArrayList();
list.Add(rectangle);
list.Add(ribbon);
ppt.Slides[0].GroupShapes(list);
ppt.SaveToFile("output.pptx", FileFormat.Pptx2010);
[VB.NET]
Dim ppt As Presentation = New Presentation()
Dim slide As ISlide = ppt.Slides(0)
Dim rectangle As IShape = slide.Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(20, 100, 200, 40))
rectangle.Fill.FillType = FillFormatType.Solid
rectangle.Fill.SolidColor.KnownColor = KnownColors.Gold
rectangle.Line.Width = 0.1F
Dim ribbon As IShape = slide.Shapes.AppendShape(ShapeType.Ribbon2, New RectangleF(60, 75, 120, 80))
ribbon.Fill.FillType = FillFormatType.Solid
ribbon.Fill.SolidColor.KnownColor = KnownColors.Purple
ribbon.Line.Width = 0.1F
Dim list As ArrayList = New ArrayList()
list.Add(rectangle)
list.Add(ribbon)
ppt.Slides(0).GroupShapes(list)
ppt.SaveToFile("output.pptx", FileFormat.Pptx2010)