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#]
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Collections;
using System.Drawing;

namespace GroupShapes
{

    class Program
    {

        static void Main(string[] args)
        {
            {
                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]
Imports Spire.Presentation
Imports Spire.Presentation.Drawing
Imports System.Collections
Imports System.Drawing

Namespace GroupShapes

	Class Program

		Private Shared Sub Main(args As String())
			If True Then
				Dim ppt As 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 New ArrayList()
				list.Add(rectangle)
				list.Add(ribbon)
				ppt.Slides(0).GroupShapes(list)
				ppt.SaveToFile("output.pptx", FileFormat.Pptx2010)


			End If
		End Sub

	End Class
End Namespace