iText最大字体大小 [英] iText Maximum Font Size

查看:918
本文介绍了iText最大字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用固定的单元格高度来创建表格。
如果字体太大,表格中看不到文字。

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.

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

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?

推荐答案

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

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.

如果您担心高度,请选择请查看 FitTextInRectangle 示例:

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;

这个例子是为了回答在矩形iText中更正文本位置中心

如果你担心的话宽度,然后你需要使用 getWidthPoint()方法,如下所述:如何计算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);

您需要确保宽度不超过细胞的宽度。为此,您需要调整 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?

这篇关于iText最大字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆