Lock Aspect Ratio

Hi

I'm dynamically creating shapes and adding them to my slide. Some of these can not be achieved using Rectangle and Ellipse e.g. Diamond and Stars so I am using a Polygon generated from A GraphicsPath to create these. All shapes have a TextFrame containing a label and I am using TextFrame.FitShapeToText to scale the shapes down/up to the size of the text.

The problem arises for custom polygons. Unlike rectangles/ellipses the aspect ratio of these shapes is essential and FitShapeToText has some interesting effects on the shape, size and location of text in custom shapes.

So two questions...

1) How to apply LockAspectRatio to a shape

2) Anybody else had any experiencing with positioning text inside custom shapes and can give any pointers on the wierd effects that occur.

Thanks in advance

Rob

Hello Rob.

It is very difficult to implement FitShapeToText, which will change both width and height of a shape. You calculate height of text, than increase width to keep aspect ratio, some wrapped lines merge and text height will decrease, so you should decrease width and recalculate text size repeatedly. That’s why PowerPoint and Aspose.Slides are using simple rule: if text wrapping is on, then FitShapeToText doesn’t touch shape’s width, otherwise height is controlled by paragraphs count and soft line breaks, shape prefer to change its width. We can implement KeepAspectRatio behavior when text wrapping is switched off, but PowerPoint ignores this flag when FitShapeToText is on, so PowerPoint will change your shape’s size in its way.

I can advice you two methods to maintain aspect ratio of generated shapes:

  1. You can switch fitShapeToText on, add text, get shape’s size (this will make Aspose.Slides to recalculate shape’s size), switch fitShapeToText off, set text alignment to center, and than fix aspect ratio. In this case, text can “overflow” shape’s area if somebody will open presentation and change the text.

  2. Switch fitShapeToText on, add text, get shape’s size and than fix aspect ratio by increasing TextFrame’s MarginLeft and MarginRight or MarginTop and MarginBottom properties. In that case, shape can loose aspect ratio if somebody will change the text.

I don’t understand which weird effects you are asking about. As far as I know, text in custom shape acts like it is in Rectangle shape.

Hi Nikolay

Thanks for the reply, I will follow your advice today and let you know how it goes.

By wierd effects I am really talking about text positioning in the shape. For example in the custom shpes the text always seems to justified so the left edge of the text is in line with the center of the shape, rather than the center of the text being centered on the shape. However this seems to be the case with Powerpoint anyway? Your reply exlpains the other oddities such as the text being one character per line, explained by the FitShapeToText not changing width.

Thanks again

Rob

There are two method to align text: TextFrame’s/TextHolder’s AnchorText property, which controls alignment of all text in the shape, but doesn’t change position of one line relative to another and Paragraph.Alignment property, which aligns lines independently. Weird effect, you described, looks like a result of left aligned paragraphs with horizontally centered AnchorText.

Hi Nikolay

Thanks again, I have resolved all my issues with text alignment and ratios of polyline shapes using the techniques you suggested. However I have no found another problem with the TextFrame in a Polyline shape.

A requirement of the presentations being created are that they are editable in Powerpoint by the user who runs the report that creates them. To this end the position, size and text of the added shapes needs to be editable.

When I add a TextFrame to a Rectangle or Ellipse then, whent he generated presentation is opened in Powerpoint, the text inside the shapes is immediately selectable and editable.

When a TextFrame is added to a Polyline shape the text is not selectable in the finished presentation. Clicking on the text just displays the properties dialog for the shape and does not let me select or edit the text inside.

How can I add text to a Polyline that is editable through Powerpoint?

When I create a shape using Insert - Pictures - AutoShape in powerpoint then the text is selectable. I haven’t found anything in the documentation that suggests I can add an AutoShape using Aspose.Slides.

Thanks

Rob

Hi Rob,

Unfortunately MS PowerPoint doesn’t allow to add text to a polyline or change it.
So creating TextFrame inside polyline is undocumented feature of PowerPoint
which is enabled with Aspose.Slides.

About AutoShapes. Current version of Aspose.Slides can’t create new autoshapes.
It will be available in the future. Now you can use simple workaround. Probably you know,
shapes can be serialized to a stream and restored. So you can create some autoshapes in
the MS PowerPoint and serialize them to files or another place. After that your application
can restore these shapes and insert to the new created presentation.

Hi again

Ok I see so Polyline is the wrong way to approach this. I am not that familiar with Powerpoint as a user so am approaching this from a programmers perspective. I know shapes can be saved from Powerpoint as a picture, and if they are saved as .emf files they maintain their vector properties. Is this the serialization you are talking about? Can these then be loaded into Aspose as Shapes and have text added to them?

I would be very grateful if you could post a short code snippet showing how to restore a shape to the presentation using Aspose.Slides and also confirm what format and how to save the shapes from Powerpoint initially.

Many Thanks

Rob

Aspose.Slides serializes shapes in own binary format. Shapes serialized and restored together with size, line and fill formatting and text. After restoring you can change text or add new.

The short example is:

1. Serialization of a shape to the MemoryStream.

MemoryStream ms = new MemoryStream();
srcShape.Serialize(ms);

2. Restoring a shape from the same stream to a slide and changing size and position.

ms.Position = 0;
Shape sh = slide.Shapes.Add(ms);
sh.X = 0;
sh.Y = 0;
sh.Width = 2000;
sh.Height = 2000;