gpt4 book ai didi

java - iText- ColumnText 在矩形中设置文本适合大小

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:13:38 28 4
gpt4 key购买 nike

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

这是我的尝试:

  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;
}

你能告诉我哪里错了吗?

另一个问题,我想测量宽度和高度,哪些文本完全包含在矩形中并且具有最大字体大小。有什么办法吗?

更新:这是对我有用的完整源代码:

   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;
}

最佳答案

主要问题

主要问题是您在 ct.setSimpleColumn 中使用了错误的参数:

ct.setSimpleColumn(10, 10, rectWidth , 70);

对比后面的cb.rectangle调用

cb.rectangle(10, 10, rectWidth , 70);

它有参数 float x, float y, float w, float h (wh 是宽度和高度)方法 ct.setSimpleColumn 具有参数 float llx、float lly、float urx、float ury(ll 位于左下方 和 < strong>ur 右上)。因此,您的 ct.setSimpleColumn 应该如下所示:

ct.setSimpleColumn(10, 10, 10 + rectWidth, 10 + 70);

附带问题

除了主要问题之外,您的结果字体大小 0.1 太大;本质上这是@David 已经指出的错误。

getMaxFontSize 方法中的主循环是这样的:

while(measureWidth < width){
measureWidth = bf.getWidthPoint(text, fontSize);
oldSize = fontSize;
fontSize += 0.1f;
}

这实际上导致 oldSize(最终返回)成为第一个适合的字体大小。您可以改用

来解决此问题
while(bf.getWidthPoint(text, fontSize) < width){
oldSize = fontSize;
fontSize += 0.1f;
}

更好的方法是只计算一次字符串宽度,根本不使用循环,例如

private static float getMaxFontSize(BaseFont bf, String text, int width)
{
int textWidth = bf.getWidth(text);

return (1000 * width) / textWidth;
}

(此方法使用整数运算。如果您坚持精确匹配,请切换为 float 或 double 运算。)

关于java - iText- ColumnText 在矩形中设置文本适合大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20769881/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com