Finding a Shape in a Slide

Skip to end of metadata
Go to start of metadata
It is important to know that PowerPoint Presentation files (PPT) 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. This topic will describe a simple technique to make it easier to find a specific shape on a slide without using its internal Id.

Finding a Shape on a Slide

All shapes added to the slides have some Alternative 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 as shown below:

Figure : Setting alternative text of a shape using MS PowerPoint


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.

Example

[C#]
//Method implementation to find a shape in a slide using its alternative text
Shape FindShape(Slide slide, string alttext)
{
    //Iterating through all shapes inside the slide
    for (int i = 0; i < slide.Shapes.Count; i++)
    {


        //If the alternative text of the slide matches with the required one then
        //return the shape
        if (slide.Shapes[i].AlternativeText.CompareTo(alttext) == 0)
            return slide.Shapes[i];
    }
    return null;
}

 
[Visual Basic]
'Method implementation to find a shape in a slide using its alternative text
Private Function FindShape(ByVal slide As Slide, ByVal alttext As String) As Shape
    Dim i As Integer


    'Iterating through all shapes inside the slide
    For i = 0 To slide.Shapes.Count - 1


        'If the alternative text of the slide matches with the required one
        'then return the shape
        If slide.Shapes(i).AlternativeTextCompareTo(alttext) = 0 Then
            Return slide.Shapes(i)
        End If
    Next

    Return Nothing
End Function

 


[C#]
//Calling FindShape method and passing the slide reference with the
//alternative text of the shape to be found
Shape shape = FindShape(slide, "Shape1");

 
[Visual Basic]
'Calling FindShape method and passing the slide reference with the
'alternative text of the shape to be found
Dim shape As Shape = FindShape(Slide, "Shape1")

 
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.