iText 最大字体大小

iText Maximum Font Size

我使用固定的单元格高度来创建 table。 如果字体太大,文本在 table.

中不可见

iText 中是否有自动将字体大小缩小到最大可能大小的内置功能,还是必须自己实现?

自动字体大小只能在 AcroForm 文本字段的上下文中使用。当您将文本字段的字体大小定义为 0 时,将选择适合矩形的字体大小。如果 table 中的单元格高度固定,您有责任确保文本适合。

如果您担心身高,请查看FitTextInRectangle示例:

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

这个例子是为了回答Correct text position center in rectangle iText

而写的

如果您担心宽度,则需要使用 getWidthPoint() 方法,如下所述:How to calculate the string width in iText?

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

您需要确保 width 不超过单元格的宽度。为此,您需要调整 myFontSize.

看我对这个问题的回答:How to choose the optimal size for a font?