Legacy notice!

iText 5 is the previous major version of iText’s leading PDF SDK. iText 5 is EOL, and is no longer developed, although we still provide support and security fixes. Switch your project to iText 8, our latest version which supports the latest PDF standards and technologies.
Check related iText 8 content!

Is there a built-in function in iText that automatically reduces the font size to the maximum possible size, or do I have to implement this by myself?

I'm using a fixed cell height to create a table. If the font size is too large, the text is not visible in the table. 

Posted on StackOverflow on Nov 9, 2015 by Clemens

Automatic font size is only possible in the context of AcroForm text fields. When you define the font size of a text field as 0, then a font size is chosen that fits the rectangle. In the case of a fixed cell height in a table, you are responsible to make sure that the text fits.

If you're concerned about the height, please take a look at the FitTextInRectangle example:

BaseFont bf = BaseFont.createFont();
int textHeightInGlyphSpace = bf.getAscent(text) - bf.getDescent(text);
float fontSize = 1000f * fixedHeight / textHeightInGlyphSpace;

If you're concerned about the width, then you need to use the getWidthPoint() method :

BaseFont bf = BaseFont.createFont();
float width = bf.getWidthPoint("My text", myFontSize);

You'll need to make sure that width doesn't exceed the width of the cell. To achieve this, you'll need to adjust myFontSize.

See my answer to this question: How to choose the optimal size for a font?