We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

FitShapeToText's bug?

Hi,
I try to utilize InsertHtml function of Aspose.Words by the code below:

Slide slide = pres.AddBodySlide();
(slide.Placeholders[0] as TextHolder).Text = “Classes”;

TextHolder ht1 = slide.Placeholders[1] as TextHolder;

Rectangle rec = slide.Shapes.AddRectangle(ht1.X, ht1.Y, ht1.Width, 1);
rec.AddTextFrame(string.Empty);
TextFrame tf = rec.TextFrame;
tf.Paragraphs.Clear();

Aspose.Words.Document doc = new Aspose.Words.Document();
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

builder.InsertHtml(html);

Body body = builder.CurrentSection.Body;

foreach (Words.Paragraph pp in body.Paragraphs)
{
Aspose.Slides.Paragraph p = new Paragraph();

foreach (Run run in pp.Runs)
{
Portion po = new Portion();
po.Text = run.Text";
p.Portions.Add(po);
}

tf.Paragraphs.Add§;
}

tf.WrapText = true;
tf.FitShapeToText = true;

string fileName = Application.StartupPath + “\hello.ppt”;
pres.Write(fileName);

MessageBox.Show("PPT file saved at " + fileName);

Process.Start(fileName);
Application.Exit();

An error is thrown at pres.Write(fileName): "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"

If I remove this line

tf.FitShapeToText = true;

evrything is OK.

Please help me on this issue. Thanks!!!

There is nothing wrong with your code and it is hard to say, where is the problem, because I am unable to reproduce it.

However, don't clear the paragraphs and remove the line tf.Paragraphs.Clear(); and then try again, does it solve your problem?

This is a weird issue. I tried this as a work around and it worked :smiley:



foreach (Words.Paragraph pp in body.Paragraphs)
{
Aspose.Slides.Paragraph p = new Paragraph();
p.Text = “”;

foreach (Run run in pp.Runs)
{
Portion po = new Portion();
po.Text = run.Text";
p.Portions.Add(po);
}



But in this case, there is another problem that I cannot control the text formatting in the TextHolder if I assign paragraph’s Text to empty and add portions to it.

Can you please tell me, which version of Aspose.Slides you are using? I ran your code on Aspose.Slides 2.8.4.0, after some modification and did not get the error. Please see it.

CODE:

Presentation pres = new Presentation();

Slide slide = pres.AddBodySlide();

(slide.Placeholders[0] as TextHolder).Text = "Classes";

TextHolder ht1 = slide.Placeholders[1] as TextHolder;

Aspose.Slides.Rectangle rec = slide.Shapes.AddRectangle(ht1.X, ht1.Y, ht1.Width, 1);

rec.AddTextFrame(string.Empty);

TextFrame tf = rec.TextFrame;

tf.Paragraphs.Clear();

//Aspose.Words.Document doc = new Aspose.Words.Document();

//Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

//builder.InsertHtml(html);

//Body body = builder.CurrentSection.Body;

for (int j = 0; j < 5; j++)

{

Aspose.Slides.Paragraph p = new Paragraph();

for (int i = 0; i < 3; i++)

{

Portion po = new Portion();

po.Text = "some text>`~!@$%^&*()_-+=[]{};;:'\"\\,.<>/?";

p.Portions.Add(po);

}

tf.Paragraphs.Add(p);

}

tf.WrapText = true;

tf.FitShapeToText = true;

pres.Write("c:\\out.ppt");

I have same issue. I’m using v2.8.7.0.

You can reproduce the issue using my example code:

Presentation pptDoc = new Presentation();
Slide slide = pptDoc.AddEmptySlide();

Shape shape = slide.Shapes.AddRectangle(200, 200, 800, 200);
shape.LineFormat.ShowLines = false;

TextFrame textFrame = shape.AddTextFrame(string.Empty);
textFrame.FitShapeToText = true;
textFrame.WrapText = false;

Paragraph para = textFrame.Paragraphs[0];
para.Portions.Add(new Portion(“a text”));
para.Portions.Clear();

pptDoc.Slides.RemoveAt(0);
string outputFilePath = “D:\temp\PptOutput.ppt”;
pptDoc.Write(outputFilePath);

Please help.

Thanks

Here is the correct code. You were clearing Portions collection after inserting, I cleared it before inserting. Also, I used Presentation.Slides.Remove() method not the other one.

Presentation pptDoc = new Presentation();

Slide slide = pptDoc.AddEmptySlide();

Shape shape = slide.Shapes.AddRectangle(200, 200, 800, 200);

shape.LineFormat.ShowLines = false;

TextFrame textFrame = shape.AddTextFrame(string.Empty);

textFrame.FitShapeToText = true;

textFrame.WrapText = false;

Paragraph para = textFrame.Paragraphs[0];

para.Portions.Clear();

para.Portions.Add(new Portion("a text"));

pptDoc.Slides.Remove(pptDoc.GetSlideByPosition(1));

string outputFilePath = "C:\\PptOutput.ppt";

pptDoc.Write(outputFilePath);