Resize a table to fit on a slide

Hi,


I am generating a document with aspose.slide (SlideEx) where tables are added to a slide.
Some tables are (depending on configuration) to large to fit on a slide (the height is too large).
When the tableheight exceeds the height of the slide I want to decrease the fontheight used in the table.
I am able to decrease the fontheight but when I read the table height afterwards I always get the same value.

int fontheight = 20;
while (tbl.Height > maxTableHeight)
{
fontheight–;
for (int iCol = 0; iCol < tbl.Columns.Count; iCol++)
{
for (int iRow = 0; iRow < tbl.Rows.Count; iRow++)
{
TextFrameEx tf = tbl[iCol, iRow].TextFrame;
tf.Paragraphs[0].Portions[0].PortionFormat.FontHeight = fontheight;
}
}
}

How can I properly decrease the fontheight in a table until the table fits the slide?

Paul

Hi Paul,


I am sorry for the delayed response.

I like to share that you need to adjust the row height manually by your self once you will reduce the font height. The RowEx.MinimalHeight property is used to set the height of table. Please use and observe the following code sample for your convenience. Please share, if I may help you further in this regard.

public static void changeTableHieght()
{
PresentationEx presEx = new PresentationEx();
TableEx table = (TableEx)presEx.Slides[0].Shapes[presEx.Slides[0].Shapes.AddTable(72, 72, new double[] { 72, 72, 144 },
new double[] { 36, 36, 36 })];
ShapeFrameEx frameBefore = table.Frame;
// resizing
table.Columns[0].Width += 10;
table.Rows[1].MinimalHeight += 14;
ShapeFrameEx frameAfterResize = table.Frame;
// changing content
table.Rows[0][0].TextFrame.Text = “line1\nline2”;
table.Rows[1][1].TextFrame.Text = “line1\nline2\nline3”;
ShapeFrameEx frameAfterContentChange = table.Frame;
// rendering
presEx.Slides[0].GetThumbnail(0.01f, 0.01f);
ShapeFrameEx frameAfterRender = table.Frame;
Assert.AreEqual(frameBefore.Width + 10, frameAfterResize.Width, “Column width change error.”);
Assert.AreEqual(frameBefore.Height + 14, frameAfterResize.Height, “Column width change error.”);
Assert.Less(frameAfterResize.Height, frameAfterContentChange.Height, “Error resizing on content change.”);
Assert.AreEqual(frameAfterContentChange.Height, frameAfterRender.Height);
}


Many Thanks,

Hi,


The MinimalHeight was already set to a small enough value.
The trick was that table.Height is not changed when the font is changed.
I implemented the following code to achieve my goal:

int fontheight = 20;
double tableHeight = tbl.Rows[0].Height * tbl.Rows.Count;
if (tableHeight > maxTableHeight)
{
foreach (RowEx row in tbl.Rows)
{
foreach (CellEx cell in row)
{
cell.MarginBottom = 0.01;
cell.MarginTop = 0.01;
}
}
sld.GetThumbnail(0.01f, 0.01f);
tableHeight = tbl.Rows[0].Height * tbl.Rows.Count;
}
while (tableHeight > maxTableHeight && fontheight > 5)
{
fontheight–;
for (int iCol = 0; iCol < tbl.Columns.Count; iCol++)
{
for (int iRow = 0; iRow < tbl.Rows.Count; iRow++)
{
TextFrameEx tf = tbl[iCol, iRow].TextFrame;
tf.Paragraphs[0].Portions[0].PortionFormat.FontHeight = fontheight;
}
}
sld.GetThumbnail(0.01f, 0.01f);
tableHeight = tbl.Rows[0].Height * tbl.Rows.Count;
}

Hi Paul,


Thanks for sharing your feedback. Can I help you further in this regard.

Many Thanks,