Shape Manipulations
Find Shape in Slide
This topic will describe a simple technique to make it easier for developers to find a specific shape on a slide without using its internal Id. It is important to know that PowerPoint Presentation files do not have any way to identify shapes on a slide except an internal unique Id. It seems to be difficult for developers to find a shape using its internal unique Id. All shapes added to the slides have some Alt Text. We suggest developers to use alternative text for finding a specific shape. You can use MS PowerPoint to define the alternative text for objects which you are planning to change in the future.
After setting the alternative text of any desired shape, you can then open that presentation using Aspose.Slides for C++ and iterate through all shapes added to a slide. During each iteration, you can check the alternative text of the shape and the shape with the matching alternative text would be the shape required by you. To demonstrate this technique in a better way, we have created a method, FindShape that does the trick to find a specific shape in a slide and then simply returns that shape.
Clone Shape
To clone a shape to a slide using Aspose.Slides for C++:
- Create an instance of the Presentation class.
- Obtain the reference of a slide by using its index.
- Access the source slide shape collection.
- Add a new slide to the presentation.
- Clone shapes from the source slide shape collection to the new slide.
- Save the modified presentation as a PPTX file.
The example below adds a group shape to a slide.
Remove Shape
Aspose.Slides for C++ allows developers to remove any shape. To remove the shape from any slide, please follow the steps below:
- Create an instance of Presentation class.
- Access the first slide.
- Find the shape with specific AlternativeText.
- Remove the shape.
- Save file to disk.
Hide Shape
Aspose.Slides for C++ allows developers to hide any shape. To hide the shape from any slide, please follow the steps below:
- Create an instance of Presentation class.
- Access the first slide.
- Find the shape with specific AlternativeText.
- Hide the shape.
- Save file to disk.
Change Shape Order
Aspose.Slides for C++ allows developers to reorder the shapes. Reordering the shape specifies which shape is on the front or which shape is at the back. To reorder the shape from any slide, please follow the steps below:
- Create an instance of Presentation class.
- Access the first slide.
- Add a shape.
- Add some text in shape’s text frame.
- Add another shape with the same co-ordinates.
- Reorder the shapes.
- Save file to disk.
Get Interop Shape ID
Aspose.Slides for C++ allows developers to get a unique shape identifier in slide scope in contrast to the UniqueId property, which allows obtaining a unique identifier in presentation scope. Property OfficeInteropShapeId was added to IShape interfaces and Shape class respectively. The value returned by OfficeInteropShapeId property corresponds to the value of the Id of the Microsoft.Office.Interop.PowerPoint.Shape object. Below is the sample code is given.
Set AlternativeText Property
Aspose.Slides for C++ allows developers to set AlternateText of any shape. To set the AlternateText of a shape, please follow the steps below:
- Create an instance of Presentation class.
- Access the first slide.
- Add any shape to the slide.
- Do some work with the newly added shape.
- Traverse through shapes to find a shape.
- Set the AlternativeText.
- Save file to disk.
Access Layout Formats for Shape
Aspose.Slides for C++ allows developers to access layout formats for a shape. This article demonstrates how you can access FillFormat and LineFormat properties for a shape.
Below is the sample code is given.
Render Shape as SVG
Now Aspose.Slides for C++ support for rendering a shape as svg. WriteAsSvg method (and its overload) has been added to Shape class and IShape interface. This method allows to save content of the shape as an SVG file. Code snippet below shows how to export slide’s shape to an SVG file.
String outSvgFileName = u"SingleShape.svg";
auto pres = System::MakeObject<Presentation>(u"TestExportShapeToSvg.pptx");
auto stream = System::MakeObject<FileStream>(outSvgFileName, FileMode::Create, FileAccess::Write);
pres->get_Slides()->idx_get(0)->get_Shapes()->idx_get(0)->WriteAsSvg(stream);
Shapes Alignment
Aspose.Slides allows to align shapes either relative to the slide margins or relative to each other. For this purpose, an overloaded SlidesUtil.AlignShapes() method has been added. The ShapesAlignmentType enumeration defines possible alignment options.
Example 1
Source code below aligns shapes with indices 1, 2 and 4 along the top border of the slide.
SharedPtr<Presentation> pres = System::MakeObject<Presentation>(u"example.pptx");
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
SharedPtr<IShape> shape1 = slide->get_Shapes()->idx_get(1);
SharedPtr<IShape> shape2 = slide->get_Shapes()->idx_get(2);
SharedPtr<IShape> shape3 = slide->get_Shapes()->idx_get(4);
SlideUtil::AlignShapes(ShapesAlignmentType::AlignTop, true, pres->get_Slides()->idx_get(0),
System::MakeArray<int32_t>(
{
slide->get_Shapes()->IndexOf(shape1),
slide->get_Shapes()->IndexOf(shape2),
slide->get_Shapes()->IndexOf(shape3)
}));
Example 2
The example below shows how to align the entire collection of shapes relative to the very bottom shape in the collection.
SharedPtr<Presentation> pres = MakeObject<Presentation>(u"example.pptx");
SlideUtil::AlignShapes(ShapesAlignmentType::AlignBottom, false, pres->get_Slides()->idx_get(0)->get_Shapes());