iText:PdfTable单元格垂直对齐
我正在尝试将标题单元格文本垂直对齐到单元格高度的中间。
这是我的代码:
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);
但这不起作用。 setHorizontalAlignment
居中,但 setVerticalAlignment
不居中。
我做错了什么吗?我怎样才能将它垂直对齐在中间?
任何帮助将不胜感激。
I'm trying to vertical align my headet cell text to be in the middle of the cell height.
This is my code:
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);
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据 Lowagie 的说法:
这在技术意义上总是正确的,但有时看起来很糟糕。
要居中,请在对象周围画一个框,找到其中心,然后将其与周围对象的中心对齐。
因此,iText 找到了短语的中心,并将其对齐。但人眼有时会关注大部分文本,例如基线和大写字母高度之间的字体部分。因此,为了让它看起来不错,你需要相对于它居中。
请注意,未使用 PdfPCell 方法 setVerticalAlignment(..)。
看起来这不一定适用于多行短语,但确实如此。
如果 iText 可以在事物周围显示边界框,问题就会很明显(注意,您可以告诉 iText 绘制边界框,它只是比神奇的开/关开关更多的工作)。
此解决方案改编自 来自 Paulo Soares 的电子邮件。
According to Lowagie:
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 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.
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.
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).
This solution is adapted from an email from Paulo Soares.
在
c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
之前添加cell.setUseAscender(true)
我和你有同样的问题,当添加上面的代码时我发现它可以工作,希望这可以解决你的问题,谢谢。
Add
cell.setUseAscender(true)
beforec1.setVerticalAlignment(Element.ALIGN_MIDDLE);
I have the same problem with you, when add code above I find it can work, hope this can solve your problem, thanks.
您可以使用选项
ExtraParagraphSpace
:You can use the option
ExtraParagraphSpace
:你不能这样做:
You cant do like this :