4

So, I have a shape I'm programatically generating, when it has a small amount of text, it looks like this:

Style is correct, vertically aligned to top

If I add a huge amount of text however, it flows out of the shape, like so:

Vertical alignment starts outside of boundaries

What I want to do is to hide the overflow and to force the text to start from the top of the shape (currently the text starts from a position higher than the top of the shape)

I haven't found much information about this so far, here is the code I'm using for the text inside the shape:

var shape = slide.Shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, left, top, width, height);
var textRange2 = shape.TextFrame.TextRange.InsertAfter(description);
            textRange2.Font.Size = 10;

shape.TextFrame.TextRange.Paragraphs().ParagraphFormat.Alignment = PpParagraphAlignment.ppAlignLeft;
shape.TextFrame.TextRange.Paragraphs().Font.Name = "Consolas";
shape.TextFrame.TextRange.Paragraphs().Font.Color.RGB = foregroundColor;

One last thing, I know I could just limit the string, but this would impose problems for the user. I want him to be able to resize the shape manually if there is too much text, so that's a no-go. Basically, I just want the equivalent of the css overflow:hidden rule.

One option for some users may be to use the following:

shape.TextFrame.AutoSize = PpAutoSize.ppAutoSizeShapeToFitText;

This will resize the shape to fit the text, there should also be an option to resize TEXT to fit the shape instead (resizing of fonts), I can't seem to find the function however.

Thanks guys

Halaster
  • 1,442
  • 1
  • 17
  • 27
  • Here, how you can use the enumeration [PpAutoSize](https://msdn.microsoft.com/en-us/library/microsoft.office.interop.powerpoint.ppautosize(v=office.14).aspx). – Darius Miliauskas Nov 01 '15 at 22:34

2 Answers2

3

So, apparently

shape.TextFrame.AutoSize 

accepts an enumerable PpAutoSize which has PpAutoSize.ppAutoSizeShapeToFitText; that can be used

whereas

shape.TextFrame2.AutoSize

accepts an enumerable MsoAutoSize which has MsoAutoSize.msoAutoSizeTextToFitShape;

So basically, if you change the textframe you're using to TextFrame2 instead of TextFrame, you can have the text resize to fit the shape automagically.

shape.TextFrame2.AutoSize = MsoAutoSize.msoAutoSizeTextToFitShape;
Halaster
  • 1,442
  • 1
  • 17
  • 27
  • OK - that's...interesting. What is the reason for Textframe2' existence? ;) Thank you for clarification. This solution does not work for my problem though since the customer does not want changing font sizes (Marketing....) – Christian Sauer May 21 '13 at 12:07
  • Honestly, I don't know why TextFrame2 exists, it seems to be very similar to the normal TextFrame but with some different properties; in your case what you need is the equivalent of overflow: hidden, right? But I can't find an option for that (and I've been searching for around two days) – Halaster May 21 '13 at 12:29
  • 1
    It's a Text Frame, isn't that obvious? :D – Christian May 28 '13 at 10:08
0

In Powerpoint directly- there is an option to do this. Based on the placement in the menu, I would guess it is in the shape.TextFrame.AutoSize Property - maybe the "mixed" Option? The PowerPoint object model is a huge mess - so it might be some other strane Property...

Christian Sauer
  • 10,351
  • 10
  • 53
  • 85
  • Hey Christian, I actually already tried that, but to no avail - it doesn't really seem to do anything (I think it's just the default setting..?) but you're right, the object model is a mess, surely the text resizing should exist since you can do it manually in Powerpoint (as you pointed out), it's just incredibly obscure and hard to find – Halaster May 21 '13 at 07:42
  • If you are unlucky there just no way to do it - there are some thing you can do in PP, but not in Automation :( – Christian Sauer May 21 '13 at 08:16
  • You're right, it could be something that you just can't do in automation, I'll find a way to do so manually and keep exploring potential options, thanks for the help Christian – Halaster May 21 '13 at 08:18
  • 1
    As an idea: It is possible to calculate the approximate size of a text in Powerpoint - if you do that, you can guess if the shape is too small and reduce the desired fontsize. I have done it for an application, but I cannot give you the code, sorry :( This could get you started: http://stackoverflow.com/questions/263614/calculate-the-display-width-of-a-string-in-c My solution built upon formattedtext: http://msdn.microsoft.com/en-us/library/system.windows.media.formattedtext.aspx – Christian Sauer May 21 '13 at 08:19
  • He-hey, thanks for that! That could be helpful! I'll keep you posted and try it out when I'm not on work hours :) – Halaster May 21 '13 at 08:27
  • Thank you - that would be very interesting! As a hint: Do not ignore the margins when you compute the size of the shape - they reduce the available space considerably. – Christian Sauer May 21 '13 at 08:31
  • Hey Chris, I posted an answer, it can be done in automation, so you don't need to waste valuable time reinventing the wheel :) – Halaster May 21 '13 at 11:33