我想将文本添加到矩形(x,y,w,h)中.文本应该适合矩形的大小(意味着它具有最大尺寸,但它仍然包含在矩形中).
我试着测量文本大小基础BaseFont.getWidthPoint()问题是最终文本大小不能适合rect.它看起来像这样:

这是我的尝试:
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
ColumnText ct = new ColumnText(writer.getDirectContent());
Font font = new Font(BaseFont.createFont());
int rectWidth = 80;
float maxFontSize = getMaxFontSize(BaseFont.createFont(), "text", rectWidth );
font.setSize(maxFontSize);
ct.setText(new Phrase("test", font));
ct.setSimpleColumn(10, 10, rectWidth , 70);
ct.go();
// draw the rect
cb.setColorStroke(BaseColor.BLUE);
cb.rectangle(10, 10, rectWidth , 70);
cb.stroke();
cb.restoreState();
// get max font size base on rect width
private static float getMaxFontSize(BaseFont bf, String text, int width){
float measureWidth = 1;
float fontSize = 0.1f;
float oldSize = 0.1f;
while(measureWidth < width){
measureWidth = bf.getWidthPoint(text, fontSize);
oldSize = fontSize;
fontSize += 0.1f;
}
return oldSize;
}
Run Code Online (Sandbox Code Playgroud)
你能告诉我我哪里错了吗?
另一个问题,我想测量宽度和高度,文本完全包含在矩形中,并具有最大的字体大小.有没有办法做到这一点?
更新:这是完整的源代码,对我有用:
private static float getMaxFontSize(BaseFont bf, String text, int width, int height){
// avoid infinite loop when text is empty
if(TextUtils.isEmpty(text)){
return 0.0f;
}
float fontSize = 0.1f;
while(bf.getWidthPoint(text, fontSize) < width){
fontSize += 0.1f;
}
float maxHeight = measureHeight(bf, text, fontSize);
while(maxHeight > height){
fontSize -= 0.1f;
maxHeight = measureHeight(bf, text, fontSize);
};
return fontSize;
}
public static float measureHeight(BaseFont baseFont, String text, float fontSize)
{
float ascend = baseFont.getAscentPoint(text, fontSize);
float descend = baseFont.getDescentPoint(text, fontSize);
return ascend - descend;
}
Run Code Online (Sandbox Code Playgroud)
主要问题是您在中使用了错误的参数ct.setSimpleColumn:
ct.setSimpleColumn(10, 10, rectWidth , 70);
Run Code Online (Sandbox Code Playgroud)
与后来的cb.rectangle电话相反
cb.rectangle(10, 10, rectWidth , 70);
Run Code Online (Sandbox Code Playgroud)
其中具有参数float x, float y, float w, float h(w并且h是宽度和高度),该方法ct.setSimpleColumn具有参数float llx, float lly, float urx, float ury(ll是左下方,而ur是右上方)。因此,您ct.setSimpleColumn应该如下所示:
ct.setSimpleColumn(10, 10, 10 + rectWidth, 10 + 70);
Run Code Online (Sandbox Code Playgroud)
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.
Your main loop in your getMaxFontSize method is this:
while(measureWidth < width){
measureWidth = bf.getWidthPoint(text, fontSize);
oldSize = fontSize;
fontSize += 0.1f;
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
(This method uses integer arithmetic. If you insist on an exact fit, switch to float or double arithmetic.)