shape.IsTextHolder is true but shape.Placeholder does not hold a TextHolder object

Hello

We use this code in our solution:

Paragraphs paragraphs = null;
if (shape.Placeholder != null && shape.IsTextHolder)
paragraphs = ((TextHolder)shape.Placeholder).Paragraphs;

Sometimes this gives the following exception:
System.InvalidCastException: Unable to cast object of type 'Aspose.Slides.Placeholder' to type 'Aspose.Slides.TextHolder'.

First I'm checking if the shape is a TextHolder (shape.IsTextHolder) but sometimes the object stored in "shape.Placeholder" isn't of type TextHolder anyway. Is this a bug? How can this be explained? If it's not a TextHolder what is it then?

Thanks for clearing my confusion.
Best regards
Thomas

Hello Thomas,

That is correct behavior but property name is not perfect. Actually it should be Shape.IsPlaceholder.
Only placeholders with text (TextHolder) supported by Aspose.Slides.
There can be other types which recognized as abstract Placeholder.

I’d suggest changing code to something like this:

TextHolder th = shape.Placeholder as TextHolder;
Paragraphs paras = th != null ? th.Paragraphs : shape.TextFrame != null ? shape.TextFrame.Paragraphs : null;

The same without “?” operator.

Paragraphs paras = null;
TextHolder th = shape.Placeholder as TextHolder;
if (th == null)
{
if (shape.TextFrame != null)

paras = shape.TextFrame.Paragraphs;

}
else
{
paras = th.Paragraphs;

}

There’s no shape.Paragraphs property. Could you send me a revised code snippet?

Sure, there should be TextFrame at first.
I have updated code snippet in the previous post.