Auto-Resize All Text within Table Cells to the Font Size of the Cell with the Smallest Font in Java

Hello,

I have a requirement whereby I want all the text within a table’s cells to be the same font size, especially after it has been auto-shrunk to fit. In this case, I was hoping to loop thru all table cells to determine the FontHeight and then use the smallest FontHeight to then modify all other cell’s FontHeights to the smallest. Basically, we don’t want different FontHeights for different cells as it does not look good.

When I manually set a cell’s FontHeight then it seems to return a height when I log it to console. However, if I don’t set FontHeight it returns NaN which is not very helpful.

tbl.getRows().get_Item(1).get_Item(1).getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat().getFontHeight()

What is the best way to achieve what I need here?

Thanks in advance,
Aaron

@mully74,
Thank you for posting the question.

Many presentation object formatting properties in PowerPoint documents use property inheritance from parent elements. When FontHeight returns NaN, it means that the font height is not set and is inherited from the paragraph. Typically you can use the getEffective method to retrieve the actual formatting property values. In your case:
tbl.getRows().get_Item(1).get_Item(1).getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat().getEffective().getFontHeight()

Examples:

Thank you Audrey, much appreciated.

However, I still cannot effectively detect whether a table cell’s text has overflowed i.e. Slides (or Powerpoint) has applied autofit and therefore automatically reduced the font to whatever.

Basically, I’m wanting to detect when a table cell’s text has been autofit (font reduced).

Is there any easy way to achieve that?

Thank you,
Aaron

@mully74,
You can try using the getAutofitType method from the ITextFrameFormat interface like this:

var textFrameFormat = autoShape.getTextFrame().getTextFrameFormat();
var autofitType = textFrameFormat.getAutofitType();

It returns a value from the TextAutofitType enumeration.

I hope this will help you.

Thanks again,

I think we’re already using AutoFitType.Normal which states

“Normal autofit. Font size and line spacing will be reduced to fit the shape.”

If I set table cell text and it it reduces the font, how do I get that “reduced” font size for each table cell?

Thanks,
Aaron

@mully74,
I am working on the question and will get back to you soon.

Thanks Audrey,

Out of interest, I am able to detect a change in cell “height” using Cell.getHeight() however, upon setting a long text value that autosizes, are not able to pickup the effective font height. Either the font stays the same size and wraps and pushes my table down OR it resizes but I don’t get new font size. And now for some reason Powerpoint won’t let me change the Text Type to “Shrink text on overflow”. I’ve even tried setting this programmatically but with no joy, but I will try again.

Might need to send you my sample template and code. That might make it easier.

Regards,
Aaron

@mully74,
Of course, please share a sample presentation and sample code.

Thanks Audrey,

Please find attached my sample source and output PPTXs as well as a screenshot.

TableTest_out.jpg (158 KB)

(Attachment TestTableCell.java is missing)

(Attachment TableTest.pptx is missing)

(Attachment TableTest_out.pptx is missing)

@mully74,
You were unable to send the files. Please zip them and share an archive file here.

Hi Audrey,

Does this work? See attached ZIP.

AsposeSuppportCase.zip (382 KB)

@mully74,
Thank you for the files. It looks like PowerPoint does not allow you to set “Shrink text on overflow” and “Wrap text in shape” options for text in table cells. Therefore, the font is never “reduced” in table cells.

Thanks Audrey, no wonder it never worked.

Inserting a TextField into every table cell will definitely solve the problem but is quite an ugly solution.

I tried inserting an Excel table object but it displayed rows/columns that I don’t want. I just wanted 3 columns and say 5 rows.

Are there any other possible solutions here?

Would HTML tables possibly work?

Regards,
Aaron

@mully74,
Unfortunately, I don’t see a workaround for the case. We apologize for any inconvenience.