您当前正在使用文本模式创建PdfPCell。这仅适用于简单的文本。见Right aligning text in PdfPCell
一旦您有更多的文本和其他类型的内容(例如图像),最好使用复合模式并使用不同的Paragraph 对象创建具有不同行的单元格而不是 Phrase(Phrase 不应该有换行符)。
您的代码将如下所示:
Dim cell1 As New PdfPCell()
cell1.Border = PdfPCell.NO_BORDER
Dim p1 As New Paragraph("line 1");
p1.Alignment = Element.ALIGN_LEFT;
cell.AddElement(p1);
Dim p2 As New Paragraph("line 2");
p1.Alignment = Element.ALIGN_CENTER
cell.AddElement(p2)
Dim p3 As New Paragraph("line 3");
p1.Alignment = Element.ALIGN_RIGHT
cell.AddElement(p3)
table.AddCell(cell1)
帮自己(尤其是那些必须维护您的代码的人)一个忙,不要使用以下行:
cell1.Border = 0
替换为:
cell1.Border = PdfPCell.NO_BORDER
同样适用:
cell1.HorizontalAlignment = 0
替换为:
cell1.HorizontalAlignment = Element.ALIGN_LEFT
您会注意到,必须阅读您的代码的人会开始更加尊重您,因为编写易于他们阅读和理解的代码就是您尊重他们的证明。
注意:只要您使用AddElement() 方法,PdfPCell 就会从 文本模式 切换到 复合模式。在复合模式中,以下行将被忽略:
cell1.HorizontalAlignment = Element.ALIGN_LEFT
在复合模式中,单元格将使用不同组件的对齐方式,以支持在单元格级别定义的对齐方式。