iText:PdfTable单元垂直对齐 [英] iText: PdfTable cell vertical alignment

查看:1050
本文介绍了iText:PdfTable单元垂直对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试垂直对齐我的headet单元格文本位于单元格高度的中间。

I'm trying to vertical align my headet cell text to be in the middle of the cell height.

这是我的代码:

    PdfPCell c1 = new PdfPCell(cerate_phrase("" ,regular_bold ));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
    c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
    table_.addCell(c1);

但这不起作用。 setHorizo​​ntalAlignment 居中但不是 setVerticalAlignment

but this does not work. setHorizontalAlignment is centered but not setVerticalAlignment.

我是做错了什么?我怎样才能在中间垂直对齐?

Am I doing something wrong? how can i vertically align it in the middle?

任何帮助都将受到赞赏。

Any help will be appreciated.

推荐答案

根据Lowagie的说法:

According to Lowagie:

PdfPCell cell = new PdfPCell(new Phrase("blah Blah blah");
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

这在技术意义上总是正确的,但有时看起来很糟糕。

This is always correct in a technical sense, but sometimes looks bad.

要居中,在对象周围画一个方框,找到它的中间,并将其与周围物体的中心对齐。

To center, draw a box around the object, find its middle, and align it with the center of its surrounding object.

iText因此找到短语的中心,并对齐它。但是人眼有时会关注大部分文本,比如基线和帽子高度之间的字体部分。所以为了让它看起来很好,你需要相对于那个。

iText thus finds the center of the phrase, and aligns it. But human eyes sometimes focus on the bulk of text, say the parts of the font between the baseline and the cap height. So to have it look good, you need to center relative to that.

Phrase content = new Phrase("Blah blah blah", Font);

Float fontSize = content.getFont().getSize();
Float capHeight = content.getFont().getBaseFont().getFontDescriptor(BaseFont.CAPHEIGHT, fontSize);

Float padding = 5f;    

PdfPCell cell = new PdfPCell(content);
cell.setPadding(padding);
cell.setPaddingTop(capHeight - fontSize + padding);

请注意,不使用PdfPCell方法setVerticalAlignment(..)。

Note that the PdfPCell method setVerticalAlignment(..) isn't used.

看起来这不一定适用于多行短语,但确实如此。

It seems like this wouldn't necessarily work for a multi-line phrase, but it does.

如果iText可以在事物周围显示边界框,那么问题就很明显了(记住,你可以告诉iText绘制边界框,它只是比神奇的开/关开关更多的工作。)

The problem would be obvious if iText could show bounding boxes around things (mind, you can tell iText to draw bounding boxes, it's just more work than a magical on/off switch).

此解决方案改编自来自Paulo Soares的电子邮件

这篇关于iText:PdfTable单元垂直对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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