FitTextToShape() can't work for me

Hi,

I'm not able to set my text to fit into the shape. Please help.

My code is as follow:

// slide 0 is the template page
Slide slide = pres.Slides[0];

Slide eventSlide = pres.CloneSlide(slide, 2);

// set body
Rectangle rectBody = (Rectangle)eventPerformanceSlide.FindShape("Body");
TextFrame txtFrameBody = rectBody.TextFrame;
txtFrameBody.FitTextToShape();
txtFrameBody.Text = sb.ToString();

Regards,

Dawn

Dear Dawn,

First set the text and then call the TextFrame.FitTextToShape method.

e.g

txtFrameBody.Text = sb.ToString();
txtFrameBody.FitTextToShape();

Also, remember, Presentation.Slides collection contains both normal and master slide. So don’t use it to get reference to normal slide, instead use Presentation.GetSlideByPosition method.

e.g

// slide 0 is the template page
Slide slide = pres.GetSlideByPosition(1);// slide position starts from 1

Below is the sample code that adds a rectangle, then add text inside it and fit the text to shape.

Presentation srcPres = new Presentation();
Slide srcSld = srcPres.GetSlideByPosition(1);
Shape shp = srcSld.Shapes.AddRectangle(100, 100, 3000, 1000);

//shp.LineFormat.ShowLines = false;
TextFrame tf = shp.AddTextFrame("");
tf.WrapText = true;
tf.Text = "This is lot of text, this is lot of text, this is lot of text, this is lot of text, this is lot of text, this is lot of text, this is lot of text, this is lot of text, this is lot of text, this is lot of text, this is lot of text, this is lot of text, this is lot of text, this is lot of text, this is lot of text, this is lot of text, this is lot of text, this is lot of text, this is lot of text, this is lot of text.";
tf.FitTextToShape();

srcPres.Write("c:\\outFitText.ppt");

Hi Shakeel,

Thanks for your reply. Your codes work.

However, I would like to read an existing TextFrame in my slide. I've modified my code but the text doesn't fit into the the rectangle. I've attached a sample powerpoint.

Rectangle rectBody = (Rectangle)eventPerformanceSlide.FindShape("Body");

TextFrame txtFrameBody = rectBody.TextFrame;

txtFrameBody.WrapText = true;

txtFrameBody.Text = sb.ToString();

txtFrameBody.FitTextToShape();

Well, I found, it is working fine. I am using Aspose.Slides 2.7.3.0 for .NET. Please see the attached ppts generated by the code below without and without FitTextToShape.

*************************************************

string strPath = @"D:\source ppts\11-11-2007\sample.ppt";

Presentation srcPres = new Presentation(strPath);

Slide eventPerformanceSlide = srcPres.GetSlideByPosition(1);

Aspose.Slides.Rectangle rectBody = (Aspose.Slides.Rectangle)eventPerformanceSlide.FindShape("Body");

TextFrame txtFrameBody = rectBody.TextFrame;

txtFrameBody.WrapText = true;

txtFrameBody.Text = "This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.This is text.";

txtFrameBody.FitTextToShape();

srcPres.Write("c:\\outFit.ppt");