在itext Pdf表格单元格中居中的文本

时间:2022-03-08 00:28:56

I need to center the text inside a Pdf table cell. Unfortunately all the text is appearing at the Bottom of the cell. Here is my sample code:

我需要把文本放在一个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);

I need the text inside the cell centered vertically inside the cell. Please help.

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

2 个解决方案

#1


4  

There are several errors in the snippet you shared. I've adapted the code and posted an example here: http://itextpdf.com/sandbox/tables/CenteredTextInCell

在您共享的代码片段中有几个错误。我修改了代码并在这里发布了一个示例:http://itextpdf.com/sandbox/tables/CenteredTextInCell

Reduced overview of changes:

减少变化的概述:

The way you define a font is wrong, or you're using a version of iText that is really, really old.

你定义字体的方式是错误的,或者你正在使用一个非常非常古老的iText版本。

The following line doesn't really make sense:

下面这句话没有什么意义:

p1.setAlignment(Element.TABLE);

There is no such value in iText (there used to be one, but it has been removed a really long time ago) and evenso it doesn't make sense to use a value reserved to define an object type as to define an alignment.

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

If you don't want a border:

如果你不想要边界:

cell.setBorder(Rectangle.NO_BORDER);

It doesn't make sense to define border widths, padding or colors:

定义边界宽度、填充或颜色是没有意义的:

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

The line you need to define vertical alignment is:

需要定义垂直对齐的线为:

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

#2


3  

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

#1


4  

There are several errors in the snippet you shared. I've adapted the code and posted an example here: http://itextpdf.com/sandbox/tables/CenteredTextInCell

在您共享的代码片段中有几个错误。我修改了代码并在这里发布了一个示例:http://itextpdf.com/sandbox/tables/CenteredTextInCell

Reduced overview of changes:

减少变化的概述:

The way you define a font is wrong, or you're using a version of iText that is really, really old.

你定义字体的方式是错误的,或者你正在使用一个非常非常古老的iText版本。

The following line doesn't really make sense:

下面这句话没有什么意义:

p1.setAlignment(Element.TABLE);

There is no such value in iText (there used to be one, but it has been removed a really long time ago) and evenso it doesn't make sense to use a value reserved to define an object type as to define an alignment.

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

If you don't want a border:

如果你不想要边界:

cell.setBorder(Rectangle.NO_BORDER);

It doesn't make sense to define border widths, padding or colors:

定义边界宽度、填充或颜色是没有意义的:

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

The line you need to define vertical alignment is:

需要定义垂直对齐的线为:

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

#2


3  

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);