更新时间:2023-09-18 19:39:16
主要问题是你在 ct.setSimpleColumn
中使用了错误的参数:
The main issue is that you use the wrong arguments in ct.setSimpleColumn
:
ct.setSimpleColumn(10, 10, rectWidth , 70);
与后来的 cb.rectangle
相比致电
cb.rectangle(10, 10, rectWidth , 70);
其参数 float x,float y,float w,float h
( w
和 h
是宽度和高度)方法 ct .setSimpleColumn
有参数 float llx,float lly,float urx,float ury
( ll 左下角和你 右上角)。因此,您的 ct.setSimpleColumn
应如下所示:
which has arguments float x, float y, float w, float h
(w
and h
being width and height) the method ct.setSimpleColumn
has arguments float llx, float lly, float urx, float ury
(ll being lower left and ur being upper right). Thus your ct.setSimpleColumn
should look like this:
ct.setSimpleColumn(10, 10, 10 + rectWidth, 10 + 70);
除了结果字体大小为0.1的主要问题;本质上这是@David已经指出的错误。
In addition to the main issue your result font size is 0.1 too large; essentially this is an error already pointed out by @David.
你的 getMaxFontSize
方法中的主循环是这样的:
Your main loop in your getMaxFontSize
method is this:
while(measureWidth < width){
measureWidth = bf.getWidthPoint(text, fontSize);
oldSize = fontSize;
fontSize += 0.1f;
}
这实质上导致 oldSize
(最终返回)是第一个不适合的字体大小。您可以通过改为使用
This essentially results in oldSize
(which eventually is returned) being the first font size which does not fit. You could fix this by instead using
while(bf.getWidthPoint(text, fontSize) < width){
oldSize = fontSize;
fontSize += 0.1f;
}
更好的方法是只计算字符串宽度一次,而不是使用循环,例如
Even better would be an approach only calculating the string width once, not using a loop at all, e.g.
private static float getMaxFontSize(BaseFont bf, String text, int width)
{
int textWidth = bf.getWidth(text);
return (1000 * width) / textWidth;
}
(此方法使用整数运算。如果您坚持精确拟合,请切换浮点数或双算术。)
(This method uses integer arithmetic. If you insist on an exact fit, switch to float or double arithmetic.)