Free Support Forum - aspose.com

setAutofitType not working on Textframes in table cells

Hi there,

I’m using aspose.slides version 6.9.0 and am testing the new setAutofitType-function for the textframes. It works fine with textframes from AutoShapeEx-Object.

What I would need is to use it for textframes in tables. I have a defined table layout with specific row heights. The text inside the cells should autoFit it’s font size so it fits into the cell without enlarging the row height. Is this not supported? Or how can I stop the cell height form growing larger with it’s content?

Thanks in advance,
Christiane Eckl

Hi Christiane Eckl,


I have observed your requirement and like to share that for tables cells we have a minimum row height or minimum column width properties for setting the size of cells that eventually becomes size of text frame. However, I request you to please share the sample application code along with generated presentation. Please also share the desired presentation for reference. I will observe the issue further on my end to help you further.

Many Thanks,

Hi,

I would like it to work like this:

final PresentationEx pres = new PresentationEx();
final SlideEx slide2 = pres.getSlides().get_Item(0);
final double[] columnWidths = new double[] { 300 };
final double[] rowHeights = new double[] { 200, 10 };
final int tableIdx = slide2.getShapes().addTable(50f, 50f, columnWidths, rowHeights);
final TableEx table2 = (TableEx) slide2.getShapes().get_Item(tableIdx);

TextFrameEx textframe = table2.get_Item(0, 0).getTextFrame();
textframe.setAutofitType(TextAutofitTypeEx.Normal);
textframe.setText(“LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE”);

pres.write(“C:\_temp\_pptx\autofit.pptx”);

This should keep the row and cell height to the defined 200px, and reduce the text’s font size until it fits into the cell without making the cell larger.

But unfortunately, this doesn’t work. The only workaround I currently found is to save the initial height and manually reduce the font size und set it to a minimalHeight until it fits. But I was hoping the setAutofitType function would do that for me.

Thanks,
Christiane

Hi Christiane,


I have observed the sample code shared by you and like to share that Text Autofit for table cells is unavailable in PowerPoint even. I have created a sample table in PowerPoint and have observed the AutoFit type options are disabled for cell text. The table cell is content driven and one can set the minimum row height or minimum column width only. Unfortunately it is not possible even in PowerPoint that you set the cell size and expect the text get auto fit in to that table cell based on its content. However, if the table cell text will grow the size of table row or column will grow accordingly. You may need to develop your own logic in this regard to decide what font height can be used to accommodate the particular amount of text in any table cell. Unfortunately, there is no such automated option available in Aspose.Slides or probably in PowerPoint as well to cater this requirement. For your kind reference, I have shared the image as well where by I have shared that AutoFit properties are disabled for cell text. Please observe the shared image AutoFitTable.png. I also like to add that AutoFit property works for normal shapes and AutoFitRectangle.png shows that properties are enabled for them. You can test the AutoShapes using following code.

public static void genTable()
{
String path2=“D:\Aspose Data\”;
final PresentationEx pres = new PresentationEx();
final SlideEx slide2 = pres.getSlides().get_Item(0);
final double[] columnWidths = new double[] { 300 };
final double[] rowHeights = new double[] { 200, 10 };
final int Idx = slide2.getShapes().addAutoShape(ShapeTypeEx.Rectangle, 200, 200, 250, 250);
final AutoShapeEx table2 = (AutoShapeEx) slide2.getShapes().get_Item(Idx);
TextFrameEx textframe = table2.getTextFrame();
textframe.setAutofitType(TextAutofitTypeEx.Normal);
textframe.setText(“LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE LONG HEADLINE”);
textframe.setAutofitType(TextAutofitTypeEx.Shape);
pres.write(path2+“autofit3.pptx”);
}


I hope that I have been able to clarify the concept and limitation before you. Please share, if I may help you further in this regard.

Many Thanks,

Hi Mudassir,

thanks for your detailed answer! I just wanted to be sure, that there’s no other way.
Maybe we can change our logic to use AutoShapeEx-Objects, or we’ll implement our own solution.

Thanks,
Christiane

Hi Christiane,


You are always welcome. Please share if I may help you further in this regard.

Many Thanks,