gpt4 book ai didi

java - itext Pdf 表格单元格中的居中文本

转载 作者:行者123 更新时间:2023-12-02 09:49:23 25 4
gpt4 key购买 nike

我需要将 Pdf 表格单元格内的文本居中。不幸的是,所有文本都出现在单元格的底部。这是我的示例代码:

String line = br.readLine();
Font f2 = new Font(Font.NORMAL, 12, Font.BOLD);
f2.setColor(Color.BLACK);
Paragraph p1 = new Paragraph(line, f2);
p1.setAlignment(Element.TABLE);
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100);
PdfPCell cell = new PdfPCell();
cell.setBorder(Rectangle.NO_BORDER);
cell.setBorderWidthBottom(1f);
cell.setUseBorderPadding(true);
cell.setPadding(0);
cell.setBorderColor(new java.awt.Color(255, 255, 255));
cell.addElement(p1);
table.addCell(cell);
output.add(table);

我需要单元格内的文本在单元格内垂直居中。请帮忙。

最佳答案

您共享的代码片段中有几个错误。我已经调整了代码并在此处发布了一个示例 http://itextpdf.com/sandbox/tables/CenteredTextInCell (死链接,现在https://github.com/itext/i5js-sandbox/blob/master/src/main/java/sandbox/tables/CenteredTextInCell.java)

减少变更概述:

您定义字体的方式是错误的,或者您使用的 iText 版本非常非常旧。

以下行实际上没有意义:

p1.setAlignment(Element.TABLE);

iText 中没有这样的值(曾经有过,但很久以前就被删除了),即使如此,使用保留的值来定义对象类型来定义对象类型也是没有意义的。对齐。

如果你不需要边框:

cell.setBorder(Rectangle.NO_BORDER);

定义边框宽度、填充或颜色没有意义:

cell.setBorderWidthBottom(1f);
cell.setUseBorderPadding(true);
cell.setBorderColor(new java.awt.Color(255, 255, 255));

定义垂直对齐方式需要的行是:

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

关于java - itext Pdf 表格单元格中的居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19703715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com