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 .NET 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 .NET:

  1. Create an instance of the Presentation class.
  2. Obtain the reference of a slide by using its index.
  3. Access the source slide shape collection.
  4. Add new slide to the presentation.
  5. Clone shapes from the source slide shape collection to the new slide.
  6. Save the modified presentation as a PPTX file.

The example below adds a group shape to a slide.

Remove Shape

Aspose.Slides for .NET allows developers to remove any shape. To remove the shape from any slide, please follow the steps below:

  1. Create an instance of Presentation class.
  2. Access the first slide.
  3. Find the shape with specific AlternativeText.
  4. Remove the shape.
  5. Save file to disk.

Hide Shape

Aspose.Slides for .NET allows developers to hide any shape. To hide the shape from any slide, please follow the steps below:

  1. Create an instance of Presentation class.
  2. Access the first slide.
  3. Find the shape with specific AlternativeText.
  4. Hide the shape.
  5. Save file to disk.

Change Shapes Order

Aspose.Slides for .NET 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:

  1. Create an instance of Presentation class.
  2. Access the first slide.
  3. Add a shape.
  4. Add some text in shape’s text frame.
  5. Add another shape with the same co-ordinates.
  6. Reorder the shapes.
  7. Save file to disk.

Get Interop Shape ID

Aspose.Slides for .NET 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 a sample code is given.

Set Alternative Text for Shape

Aspose.Slides for .NET allows developers to set AlternateText of any shape. Shapes in a presentation could be distinguished by the AlternativeText or Shape Name property. AlternativeText property could be read or set by using Aspose.Slides as well as Microsoft PowerPoint. By using this property, you can tag a shape and can perform different operations as Removing a shape, Hiding a shape or Reordering shapes on a slide. To set the AlternateText of a shape, please follow the steps below:

  1. Create an instance of Presentation class.
  2. Access the first slide.
  3. Add any shape to the slide.
  4. Do some work with the newly added shape.
  5. Traverse through shapes to find a shape.
  6. Set the AlternativeText.
  7. Save file to disk.

Access Layout Formats for Shape

Aspose.Slides for .NET provides a simple API to access layout formats for a shape. This article demonstrates how you can access layout formats.

Below sample code is given.

Render Shape as SVG

Now Aspose.Slides for .NET 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.

Shapes Alignment

Aspose.Slides allows to align shapes either relative to the slide margins or relative to each other. For this purpose, overloaded method SlidesUtil.AlignShape() 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.

using (Presentation pres = new Presentation("example.pptx"))
{
     ISlide slide = pres.Slides[0];
     IShape shape1 = slide.Shapes[1];
     IShape shape2 = slide.Shapes[2];
     IShape shape3 = slide.Shapes[4];
     SlideUtil.AlignShapes(ShapesAlignmentType.AlignTop, true, pres.Slides[0], new int[]
     {
          slide.Shapes.IndexOf(shape1),
          slide.Shapes.IndexOf(shape2),
          slide.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.

using (Presentation pres = new Presentation("example.pptx"))
{
    SlideUtil.AlignShapes(ShapesAlignmentType.AlignBottom, false, pres.Slides[0].Shapes);
}