2

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.

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?

Beri
  • 11,470
  • 4
  • 35
  • 57
Clemens
  • 99
  • 1
  • 10

1 Answers1

1

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;

This example was written in answer to Correct text position center in rectangle iText

If you're concerned about the width, then you need to use the getWidthPoint() method as explained here: How to calculate the string width in iText?

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?

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • is there a way to force to show the text in the table/cell, although the font size is to big and/or the string is to long? – Clemens Nov 09 '15 at 17:12
  • What would that look like in your opinion? Can you provide a sketch that shows how to put text that is too big into a rectangle that is too small? – Bruno Lowagie Nov 09 '15 at 17:23
  • @Clemens I've found an old answer to a question that may be similar to yours. I'll update my answer with a link to that question (and answer). – Bruno Lowagie Nov 10 '15 at 16:31