iText:PdfTable单元格垂直对齐

发布于 2024-10-29 19:28:40 字数 468 浏览 1 评论 0原文

我正在尝试将标题单元格文本垂直对齐到单元格高度的中间。

这是我的代码:

    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 不居中。

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

任何帮助将不胜感激。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

山有枢 2024-11-05 19:28:40

根据 Lowagie 的说法:

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

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

要居中,请在对象周围画一个框,找到其中心,然后将其与周围对象的中心对齐。

因此,iText 找到了短语的中心,并将其对齐。但人眼有时会关注大部分文本,例如基线和大写字母高度之间的字体部分。因此,为了让它看起来不错,你需要相对于它居中。

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

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

Typography LineTerms

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

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

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

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.

Typography Line Terms

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.

作死小能手 2024-11-05 19:28:40

c1.setVerticalAlignment(Element.ALIGN_MIDDLE);之前添加cell.setUseAscender(true)
我和你有同样的问题,当添加上面的代码时我发现它可以工作,希望这可以解决你的问题,谢谢。

Add cell.setUseAscender(true) before c1.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.

锦爱 2024-11-05 19:28:40

您可以使用选项ExtraParagraphSpace

c1.HorizontalAlignment = Element.ALIGN_CENTER;
c1.VerticalAlignment = Element.ALIGN_MIDDLE;
c1.ExtraParagraphSpace = 2;

You can use the option ExtraParagraphSpace:

c1.HorizontalAlignment = Element.ALIGN_CENTER;
c1.VerticalAlignment = Element.ALIGN_MIDDLE;
c1.ExtraParagraphSpace = 2;
一向肩并 2024-11-05 19:28:40

你不能这样做:

cell.setBackgroundColor(BaseColor.GRAY);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setUseAscender(true);

You cant do like this :

cell.setBackgroundColor(BaseColor.GRAY);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setUseAscender(true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文