Shapes are a fine way of making your PowerPoint presentations more elaborative and appealing. PowerPoint provides a wide range of shapes that you can add to your presentation slides i.e. ellipses, lines, rectangles, connectors, and so on. In order to automate this feature, this article covers how to add, clone, and remove shapes in PowerPoint slides programmatically using C#.
- C# API to Work with PowerPoint Shapes
- Types of PowerPoint Shapes
- Add Shape to PowerPoint Slides
- Add Connector to Shapes in PowerPoint Slides
- Clone a Shape in PowerPoint Slides
- Remove Shapes from PowerPoint Slides
C# API to Work with PowerPoint Shapes
Aspose.Slides for .NET is a C# API which is designed to work with PowerPoint presentations from within the .NET applications. Along with other presentation manipulation features, the API provides easy ways of working with shapes in PowerPoint slides. You can either download API’s DLL or install it via NuGet.
Install-Package Aspose.Slides.NET
PowerPoint Shapes
Aspose.Slides for .NET supports a wide range of shape types you can add to PowerPoint slides. The most commonly used shapes include:
Add a Shape to PowerPoint Slides using C#
In order to add a shape i.e. ellipse, line, rectangle, etc., Aspose.Slides provides IShapeCollection.AddAutoShape(ShapeType, Single, Single, Single, Single) method. The ShapeType enum lets you specify the type of shape you want to add. The following are the steps to add a shape to a PowerPoint slide.
- Create an instance of Presentation class to create a new presentation or load an existing one.
- Obtain the reference of a slide using Presentation.Slides[index] into ISlide object.
- Add an Ellipse (or any other shape) using IShapeCollection.AddAutoShape(ShapeType, Single, Single, Single, Single) method exposed by IShapes object.
- Save the PPTX file using Presentation.Save(String, SaveFormat) method.
The following code sample shows how to add a shape into a PowerPoint slide using C#.
Add Connector to Connect PowerPoint Shapes in C#
A connector is a line that is used to connect shapes in order to join them. A connector can be a straight or a curved line. Let’s see how to add a connector between two shapes in a PowerPoint slide.
- Create an instance of the Presentation class to create a new presentation.
- Obtain the reference of a slide using Presentation.Slides[index] into ISlide object.
- Add two shapes just like you have added in the previous example and get their references in IAutoShape objects.
- Create a new IConnector object using IShapeCollection.AddConnector(ShapeType, Single, Single, Single, Single) method.
- Join the shapes using IConnector.StartShapeConnectedTo and IConnector.EndShapeConnectedTo properties.
- Call IConnector.Reroute() method to create the shortest automatic connection path.
- Save the PPTX file using Presentation.Save(String, SaveFormat) method.
The following code sample shows how to connect shapes in a PowerPoint slide using C#.
Clone Shapes in PowerPoint Slides using C#
You can also clone shapes from one PowerPoint slide to another using Aspose.Slides for .NET. The following are the steps to perform this operation.
- Create an instance of the Presentation class.
- Obtain the reference of a slide using Presentation.Slides[index] into ISlide object.
- Access the source slide shapes using ISlide.Shapes collection.
- Access the destination slide shapes using ISlide.Shapes collection.
- Clone shapes from the source slide shape collection to destination slide using IShapeCollection.AddClone(ISlide) method.
- Save the updated presentation file.
The following code sample shows how to clone shapes within PowerPoint slides using C#.
Remove Shapes from PowerPoint Slides using C#
The following are the steps to remove shapes from a PowerPoint slide.
- Create an instance of the Presentation class to load the PPTX file.
- Access the desired slide from Presentation.Slides[index] into ISlide object.
- Find the shape with a specific IShape.AlternativeText.
- Remove the shape using ISlide.Shapes.Remove(IShape) method.
- Save the updated presentation file.
The following code sample shows how to remove shapes from a PowerPoint slide using C#.
Conclusion
MS PowerPoint lets you use various types of shapes in order to make your slides more elaborative. You can use ellipses, rectangles, lines, etc. to draw your shapes and connectors to join them. With the help of steps and code samples, this article covered how to add, clone, and remove shapes in PowerPoint slides programmatically using C#. In case you would like to explore more about the API, you can visit the documentation.