Work with Shapes in Slides in C#.jpg

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

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.

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.

  1. Create an instance of the Presentation class to create a new presentation.
  2. Obtain the reference of a slide using Presentation.Slides[index] into ISlide object.
  3. Add two shapes just like you have added in the previous example and get their references in IAutoShape objects.
  4. Create a new IConnector object using IShapeCollection.AddConnector(ShapeType, Single, Single, Single, Single) method.
  5. Join the shapes using IConnector.StartShapeConnectedTo and IConnector.EndShapeConnectedTo properties.
  6. Call IConnector.Reroute() method to create the shortest automatic connection path.
  7. 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.

  1. Create an instance of the Presentation class.
  2. Obtain the reference of a slide using Presentation.Slides[index] into ISlide object.
  3. Access the source slide shapes using ISlide.Shapes collection.
  4. Access the destination slide shapes using ISlide.Shapes collection.
  5. Clone shapes from the source slide shape collection to destination slide using IShapeCollection.AddClone(ISlide) method.
  6. 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.

  1. Create an instance of the Presentation class to load the PPTX file.
  2. Access the desired slide from Presentation.Slides[index] into ISlide object.
  3. Find the shape with a specific IShape.AlternativeText.
  4. Remove the shape using ISlide.Shapes.Remove(IShape) method.
  5. 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.

See Also