在矩形iText中正确的文本位置中心 [英] Correct text position center in rectangle iText

查看:72
本文介绍了在矩形iText中正确的文本位置中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在适合矩形大小的矩形内绘制文本,例如我的上一个问题,我希望文本在矩形中居中对齐.
问题是显示文本的Y坐标错误,如下图所示:

这是我的代码:

I try to draw text inside rectangle which fit rectangle size, like my previous question, I want text align center in rectangle.
The problem is display text has wrong Y coordinate, look like this one:

And here is my code:

    PdfContentByte cb = writer.getDirectContent();

        Rectangle rect = new Rectangle(100, 150, 100 + 120, 150 + 50);
        cb.saveState();
        ColumnText ct = new ColumnText(writer.getDirectContent());
        Font font = new Font(BaseFont.createFont());
        float maxFontSize;
        // try to get max font size that fit in rectangle
        font.setSize(maxFontSize);
        ct.setText(new Phrase("test", font));           
        ct.setSimpleColumn(rect.getLeft(), rect.getBottom(), rect.getRight(), rect.getTop());
        ct.go();        

        // draw the rect
        cb.setColorStroke(BaseColor.BLUE);
        cb.rectangle(rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight());
        cb.stroke();
        cb.restoreState();

我什至画这样的文字:

        cb.saveState();
        cb.beginText();
        cb.moveText(rect.getLeft(), rect.getBottom());       
        cb.setFontAndSize(BaseFont.createFont(), maxSize);
        cb.showText("test");
        cb.endText();
        cb.setColorStroke(BaseColor.BLUE);
        cb.rectangle(rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight());
        cb.stroke();

得到结果:

And got the result:

所以我想知道itext如何基于坐标渲染文本?因为我对文本和矩形绑定使用相同的矩形框架.

So I wonder how can itext render text base on the coordinates? Because I use the same rectangle frame for text and rectangle bound.

推荐答案

我不确定我是否正确理解了您的问题.我假设您想将一些文本垂直地垂直放置到一个矩形中,但是我不明白如何计算字体大小,也看不到您在任何地方设置前导(可以通过使用ColumnText.showAligned()来避免)

I'm not sure if I understand your question correctly. I'm assuming you want to fit some text into a rectangle vertically, but I don't understand how you calculate the font size, and I don't see you setting the leading anywhere (which you can avoid by using ColumnText.showAligned()).

我创建了一个名为 FitTextInRectangle 的示例,该示例会生成PDF chunk_in_rectangle.pdf .由于舍入因素(我们正在使用float值),单词test略大于矩形,但是代码显示了如何计算字体大小以使文本或多或少地适合矩形.

I've created an example named FitTextInRectangle which results in the PDF chunk_in_rectangle.pdf. Due to rounding factors (we're working with float values), the word test slightly exceeds the rectangle, but the code shows how to calculate a font size that makes the text fit more or less inside the rectangle.

在代码示例中,基线由使用ColumnText时的前导定义(并且前导是错误的)或使用showText()时矩形的底部坐标定义(而您忘记考虑了降序).

In your code samples, the baseline is defined by the leading when using ColumnText (and the leading is wrong) or the bottom coordinate of the rectangle when using showText() (and you forgot to take into account value of the descender).

这篇关于在矩形iText中正确的文本位置中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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