Free Support Forum - aspose.com

Table size changes after text change


After changing the text inside of a table cell, the table is changing size on me and I don't know why. It seems like it is growing just a bit, but that small amount is throwing off our layout. I have attached a sample presentation.

Here is my code:

Sub Main()

Dim license As New Aspose.PowerPoint.License
license.SetLicense(AsposeLicensePath)

Dim powerPointDocument As New Aspose.PowerPoint.Presentation("C:\temp\SmallTable.ppt")

Dim slide As Aspose.PowerPoint.Slide = powerPointDocument.Slides(0)

For Each shape As Aspose.PowerPoint.Shape In slide.Shapes
ReplaceTextInShape(shape)
Next

powerPointDocument.Write("C:\temp\BigTable.ppt")

End Sub

Private Sub ReplaceTextInShape(ByVal shape As Aspose.PowerPoint.Shape)
If shape.GetType.Equals(GetType(Aspose.powerPoint.Table)) Then
Dim tableShape As Aspose.PowerPoint.Table = DirectCast(shape, Aspose.PowerPoint.Table)
For Each innerTableShape As Aspose.PowerPoint.Shape In tableShape.Shapes
ReplaceTextInShape(innerTableShape)
Next
ElseIf Not shape.IsTextHolder AndAlso Not shape.TextFrame Is Nothing Then
Dim paragraphs As Aspose.PowerPoint.Paragraphs = shape.TextFrame.Paragraphs
For Each paragraph As Aspose.PowerPoint.Paragraph In paragraphs
For Each portion As Aspose.PowerPoint.Portion In

paragraph.Portions

    portion.Text = portion.Text +

“XXX”

Next

Next

End If

End Sub

Hello trip42.

If text doesn’t fit to cell Aspose.PowerPoint changes row’s height. This is a correct behaviour.


Ok, but even if the new text string is shorter than the original text string, the table still grows. For instance, you can take my previously posted example and just make this change:

change:
portion.Text = portion.Text + "XXX"

to:
portion.Text = "X"

And you will still see the problem. Any other ideas?

Thanks for feedback. We’ve changed text size calculation and I bet it will available in the next hotfix.

I also had a small problem with table resizing except my problem was the opposite where the text spilled over the table boundries when I opened it in PowerPoint.

I had a table that had 3 rows where each cell contained a titled list like:

    Title
  • one
    Title
  • one
    Title
  • one

I used similar code as what you have where the only notible difference is that I changed:

For Each portion As Aspose.PowerPoint.Portion In paragraph.Portions
portion.Text = portion.Text + "XXX"
Next

To the following:

For Each portion As Aspose.PowerPoint.Portion In paragraph.Portions
If portion.Text = "one" Then
portion.Text = "one\rtwo\rthree\r"
Next

Which makes each cell in the table look like:

    Title
  • one
  • two
  • three

When I open the changed .ppt in PowerPoint, it appears that the individual table cells have resized but the table as a whole has not so the text spill over the table lines. Shouldn't the table resize as well?

Hello JMiller.

You must not to assign string with the carriage return character. You
can use soft line break (shift-enter in MS PowerPoint, ‘\v’ in C#),
which starts new string without breaking paragraph, оr you can create
new paragraph for each new line. If you want new lines to have bullets
creating new paragraphs is the only way.



something like


                    for(int i = 0; i < paragraphs.Count; i++)
for(int j = 0; j < paragraphs[ i ].Portions.Count; j++)
if(paragraphs[ i ].Portions[ j ].Text == "one")
{
if(paragraphs[ i ].Portions.Count > j + 1)
{
// first task - breaking current paragraph.
Paragraph newParagraph = new Paragraph(paragraphs[ i ]);
// removing following portions from current paragraph
while(paragraphs[ i ].Portions.Count > j + 1)
paragraphs[ i ].Portions.RemoveAt(j + 1);
// removing preceeding portions from the second paragraph
int numberToRemove = j + 1;
while(numberToRemove-- > 0)
newParagraph.Portions.RemoveAt(0);
paragraphs.Insert(i + 1, newParagraph);
}
// adding new paragraphs
string[] toAdd =
{
"two",
"three",
"four"
};
foreach(string str in toAdd)
{
paragraphs.Insert(i + 1, new Paragraph(paragraphs[ i ]));
i++;
Portions portions = paragraphs[ i ].Portions;
while(portions.Count > 1)
portions.RemoveAt(0);
portions[ 0 ].Text = str;
}
}

Of course, you can simplify code if you have not to preserve portions after “one” in paragraph.