Free Support Forum - aspose.com

How to measure text size?

Hi There.

We need to accurately measure text size (height and width) in our slides. Browsing through the API, we could not find any function/method that would return the size of a string given its font.

Could you please give us a hand here? Any clue would be appreciated.

Regards,

Fereshteh

Dear Fereshteh,

You can get the font height of the given text inside a textframe using TextFrame.Paragraphs[parIndex].Portions[portIndex].FontHeight property.

Also, you can add a TextFrame with some predefined width and set its TextFrame.FitShapeToText property true, so that it could automatically grow downward. After inserting your text in such a textframe, you could determine approximate height of your text given its font using TextFrame.Height property.

However, to get exact height (including or excluding line spacing) or width of the text given its font, you will have to play with Font class or related classes in System.Drawing namespace.

You can find more information over the internet, just to give you idea, I have pasted one such a link.

http://www.thescripts.com/forum/thread254703.html

It would also be great, if you could paste some example here it, if you chose to use System.Drawing namespace. Thanks for your favor.

In PDF, GetStringWidth() can be used to measure the string width, is there is any other similar function available in PPT?

Thanks,

Regilesh

I’d like to bring this up again, as it’s been 2 years since the original post and I guess you already addressed this issue.

Elaborating a little:
in my case, I have a base “label” in a template file with a fixed width set. If, after assigning the text, the height didn’t change (1 line) I set wrap-text to false so the text-frame collapses. The problem lies with texts that take more than 1 line but doesn’t make use of all the text-frame’s width (think 1 medium word + 1 long word)… in those cases I need to resize the shape so it better fits the text it contains (and avoid wasted space inside the text-frame).

Thanks for any information.

Hi,

If you provide the template supported with an example as you mentioned, it will be easier to proceed.

Not a problem, here you have it…

Hi,

In fact, you are using a Text Box in your template which does not resize through TextFrame.FitShapeToText property. There are two ways to achieve the desired results.

1) Update your template by converting Text Box to Rectangle. After that, add a text frame to the rectangle and set text frame properties through Aspose.

2) With the same template, create a rectangle having similar formatting as that of textbox. An example is as follows:

Presentation pres = new Presentation("d:\\ppt\\ze\\txtSpace_2.ppt");

Slide sld = pres.GetSlideByPosition(1);

int intX = 0;

int intY = 0;

int intHt = 0;

int intWt = 0;

Color bkcol = new Color();

Color frcol = new Color();

Color lncol = new Color();

foreach (Aspose.Slides.Shape shp in sld.Shapes)

{

if (shp is Aspose.Slides.Rectangle)

{

Aspose.Slides.Rectangle rect = (Aspose.Slides.Rectangle)shp;

intX = rect.X;

intY = rect.Y;

intHt = rect.Height;

intWt = rect.Width;

bkcol = rect.FillFormat.BackColor;

frcol = rect.FillFormat.ForeColor;

lncol=rect.LineFormat.ForeColor;

rect.Width = 0;

}

}

Aspose.Slides.Rectangle rect1 = sld.Shapes.AddRectangle(intX,intY,intWt,intHt);

rect1.FillFormat.Type = FillType.Solid;

rect1.FillFormat.BackColor = bkcol;

rect1.FillFormat.ForeColor = frcol;

rect1.LineFormat.ForeColor = lncol;

TextFrame tf = rect1.AddTextFrame("Somewhat_short_text");

Paragraph para2 = new Paragraph();

Portion port2 = new Portion();

tf.Paragraphs.Add(para2);

tf.Paragraphs[1].Portions.Add(port2);

tf.Paragraphs[1].Portions[0].Text = "Text_that_causes_the_new_line.";

tf.FitShapeToText = true;

pres.Write("d:\\ppt\\ze\\txtSpc_2.ppt");

Thanks for your replay, but that doesn’t help.

Let’s say you have a 1-line Text Box (or a Rectangle) that has the option ‘Wrap text in shape’ checked, and fits perfectly up to “the_new”, but then you write "_line ". What will happen is that it will break the text into 2 lines, leaving a big empty space at the right. That’s the space I want to get rid of.

If I could get the size (rectangle) actually used by the text I could adjust the shape’s size, so that’s what I’m looking for. It seems you have methods to calculate the text size but weren’t exposed, has this changed by now? What are those methods?

P.S. “Next time automatically sign me in” doesn’t work (at least, not in Firefox).

Edit: I’ve added a new sample presentation for better understanding.

Hi,

1) You can always control lines through paragraphs. If you mean you are not sure of what number of characters will fit in the width fixed for a rectangle, that will certainly require some property/method exposed by TextFrame object for calculation of the text width which is not available yet. Please make correction if i am wrong.

2) "Next time automatically sign in" works well with Internet Explorer. For Firefox, there is some problem. I shall report it to our web development team.

  1. Well, I don't need to know how many characters are in each line (though I guess that could be useful for some people) but some way to know how wide, in the same measurement unit (px), is the actual text.

    Something that tells me how wide is each line would accomplish that pretty well.

  2. OK, thank you.

Hi,

An issue with issue id 11830 has been created for provision of the text width in a TextFrame. You will be informed in this thread as soon as there is some progress in this regard.

Great, thank you.

I just noticed you released an update today, do you have an idea of when could it be released? —I understand it’s “just” to expose some internal methods.

Well,

I shall have to consult with the development team and after consultation, I can give you an idea about it.

So… do you have an estimation for the next release?

Hi,

I have discussed the issue with our development team and found that the feature requires additional development. So, there is possibility that implementaion of this feature will require more time.