Microsoft PowerPoint provides you the ability to add shapes to your presentations. Shapes can be helpful in scenarios such as showing the flow of data or showing different phases of a process. You can use shapes such as ellipses, lines, rectangles, etc., and connect them using connectors. You might find yourself in scenarios where you have to add shapes to PowerPoint slides programmatically. To that end, this article will teach you how to work with shapes in PowerPoint presentations using C++.
- C++ API for Working with Shapes in PowerPoint Presentations
- Add a Shape to a PowerPoint Slide
- Add Connected Shapes to a PowerPoint Slide
- Clone Shapes in PowerPoint Slides
- Remove Shapes from PowerPoint Slides using C++
- Supported PowerPoint Shapes
- Get a Free License
C++ API for Working with Shapes in PowerPoint Presentations
Aspose.Slides for C++ is a native C++ library that supports creating, reading, and manipulating PowerPoint files. The API also supports working with shapes in PowerPoint presentations. You can either install the API through NuGet or download it directly from the Downloads section.
PM> Install-Package Aspose.Slides.Cpp
Add a Shape to a PowerPoint Slide
In order to add a shape, use the ISlide->get_Shapes()->AddAutoShape() method provided by the API. The following are the steps to add a shape to a PowerPoint slide.
- Firstly, load the PowerPoint presentation using the Presentation class.
- Retrieve the slide where you want to add the shape using the Presentation->get_Slides()->idx_get (int32_t index) method.
- Add the shape using the ISlide->get_Shapes()->AddAutoShape (ShapeType shapeType, float x, float y, float width, float height) method.
- Finally, save the presentation using the Presentation->Save (System::String name, Export::SaveFormat format) method.
The following is the sample code to add a shape to a PowerPoint slide using C++.
Add Connected Shapes to a PowerPoint Slide
Connectors can be used to connect shapes. In order to create a connector, you can use the ISlide->get_Shapes()->AddConnector() method. The following are the steps to add connected shapes to a PowerPoint slide.
- Firstly, load the PowerPoint presentation using the Presentation class.
- Retrieve the slide where you want to add the shapes using the Presentation->get_Slides()->idx_get (int32_t index) method.
- Add the shapes using the ISlide->get_Shapes()->AddAutoShape (ShapeType shapeType, float x, float y, float width, float height) method.
- Add the connector using the ISlide->get_Shapes()->AddConnector (ShapeType shapeType, float x, float y, float width, float height) method.
- Connect the shapes to the connector using the IConnector->set_StartShapeConnectedTo (System::SharedPtr<IShape> value) and IConnector->set_EndShapeConnectedTo (System::SharedPtr<IShape> value) methods.
- Call the IConnector->Reroute() method to create the shortest automatic connection path.
- Finally, save the presentation using the Presentation->Save (System::String name, Export::SaveFormat format) method.
The following is the sample code to add connected shapes to a PowerPoint slide using C++.
Clone Shapes in PowerPoint Slides
You can also clone existing shapes using the Aspose.Slides for C++ API. In order to clone a shape, use the ShapeCollection->InsertClone() method provided by the API. The following are the steps to clone shapes from one slide to another.
- Firstly, load the PowerPoint presentation using the Presentation class.
- Retrieve the source slide using the Presentation->get_Slides()->idx_get (int32_t index) method.
- Access the shapes of the source slide using the ISlide->get_Shapes() method.
- Access the shapes of the destination slide using the ISlide->get_Shapes() method.
- Clone the shape using the IShapeCollection->InsertClone (int32_t index, System::SharedPtr<IShape> sourceShape, float x, float y) method.
- Finally, save the presentation using the Presentation->Save (System::String name, Export::SaveFormat format) method.
The following is the sample code to clone shapes in PowerPoint slides using C++.
Remove Shapes from PowerPoint Slides using C++
The following are the steps to remove shapes from PowerPoint slides.
- Firstly, load the PowerPoint presentation using the Presentation class.
- Retrieve the slide from where you want to remove the shape using the Presentation->get_Slides()->idx_get (int32_t index) method.
- Find the required shape by matching the alternative text using the IShape->get_AlternativeText() method.
- Remove the shape using the ISlide->get_Shapes()->Remove (System::SharedPtr<IShape> shape) method.
- Finally, save the presentation using the Presentation->Save (System::String name, Export::SaveFormat format) method.
The following is the sample code to remove shapes from PowerPoint slides using C++.
Supported PowerPoint Shapes
Aspose.Slides for C++ supports many shapes for you to work with. The following is a list of some of the supported shapes.
You can view the complete list of supported shapes by viewing the ShapeType enum values.
Get a Free License
You can request a free temporary license to try the API without evaluation limitations.
Conclusion
In this article, you have learned how to work with shapes in PowerPoint presentations using C++. Specifically, you have learned how to add, clone, and remove shapes from PowerPoint slides. Furthermore, you have seen how to connect shapes using a connector. In addition to working with shapes, Aspose.Slides for C++ provides many additional features for enhancing your PowerPoint presentations. You can explore the API in detail by visiting the official documentation. In case of any questions, please feel free to reach us on our free support forum.