iText- ColumnText在Rectangle中设置文本适合大小 [英] iText- ColumnText set text fit size in Rectangle

查看:1378
本文介绍了iText- ColumnText在Rectangle中设置文本适合大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文本添加到矩形(x,y,w,h)中。文本应该是矩形的大小(意味着它有一个最大的大小,但它仍然包含在矩形中)。

我试图测量文本大小基于 BaseFont.getWidthPoint()问题是最终文本大小不适合rect。它看起来像这样:

I want to add text into a rectangle(x,y,w,h). Text should be fitted size of rectangle (mean it has a maximum size but it still contains in rectangle).
I tried to measure the text size base on BaseFont.getWidthPoint() The problem is the final text size can't fit the rect. It looks like this:

这是我的尝试:

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

你能否告诉我哪里错了?

Could you please tell me where I am wrong?

另一个问题,我想测量宽度和高度,文本完全包含在矩形中并具有最大字体大小。有没有办法做到这一点?

Another problem, I want to measure for both width and height, which text completely contains in rectangle and has the maximum font size. Is there any way to do this?

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

Update: here is the complete source code that worked for me:

   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 中使用了错误的参数:

The main issue

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已经指出的错误。

A side issue

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.)

这篇关于iText- ColumnText在Rectangle中设置文本适合大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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