Getting the Parent Shape from a just TextFrame object

I’m in the processing of converting an old C# Aspose Slides application that is using the old v7 Slides Api to the current version. One of the things we have used very heavily in our code was passing just a TextFrame through to methods and being able to get at the parent Shape through the property TextFrame.ParentShape .

Is there a new API way to get at this property? You see in the Visual Studio Debugger below that this property exists internally

image.png (5.4 KB)

but I can’t figure out how to easily get at this property.

Thanks in advance

Michael Bond

@Michael_Bond

You are right that at present there is no public property returning the parent shape. I have created a ticket with ID SLIDESNET-42701 in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be notified once the issue will be fixed.

1 Like

@Michael_Bond

We have investigated the requirements on our end. Actually, the TextFrame is an integral part of the AutoShape, therefore we can not to provide an open access to a parent shape. Can you please share an example of code that what you want to achieve and we will try to analyze and give back a workaround of this situation.

In our application we use tagged TextBoxes as markers and then either data from the database replaces the tag or the text box is removed altogether and the boundaries are used to locate an image, chart etc.
Below is some actual code from the older API that illustrates the problem:

tf is a TextFrame that is passed through from much earlier in the method chain

//These Co-ordinates were accessible on the TextFrame but have been moved to the Shape
int xCoOrd = tf.X;
int yCoOrd = tf.Y;
int tfHeight = tf.Height;
int tfWidth = tf.Width;

//Remove the Text Frame
Slide parentSlide = (Slide)tf.ParentShape.ParentSlide;
parentSlide.Shapes.Remove(tf.ParentShape);
//Replace at X and Y with the height and width the new Shape

The parentSlide is possible to access now via tf.AsISlideComponent.Slide but it seems odd that the parentShape is no-longer accessible.

Unless there is a workaround that I’m not aware of it does appear as if the TextFrame that is passed around will have to be replaced with the Shape but that is certainly a non trivial piece of work.

( One theory we had was that as I have access to the Slide instead of the Shape we could search the Shapes of collection on the Slide using LINQ for the shape with this TextFrame, but that is certainly a very inefficient work-around.)

@Michael_Bond

Thank you for sharing the details. We will share feedback with you as soon as it will be shared by our team.

@Michael_Bond

I have a made a utility code that can help you in getting the required shape of text frame. Unfortunately, the property parent shape of text frame cannot be made public owing to many internal reasons. But following sample code will help you in achieving the desired.

public static IShape FindShape(ITextFrame textFrame)
        {
            foreach (IShape shape in textFrame.Slide.Shapes)
            {
                if (shape is IAutoShape)
                {
                    IAutoShape autoShape = (IAutoShape)shape;
                    if (autoShape.TextFrame == textFrame)
                    {
                        return autoShape;
                    }
                }
            }
            return null;
        }
1 Like

Wow! That’s simply amazing work! The only change I am going to make to your code is to turn it into an extension method called ParentShape() so that I can simply search/replace .ParentShape into .ParentShape() but never-the-less great work!

@Michael_Bond

It’s good to know that suggested approach has worked on your end.