我正在创建一个表,其中每列都有自己的对齐方式,如下所示。如何在列级别而不是单元格级别完成它?
iText 和 iTextSharp 不支持列样式和格式。做到这一点的唯一方法就是像您当前正在做的那样,逐个单元地进行。
EDIT
最简单的解决方法是创建设置公共属性的辅助方法。这些可以通过扩展方法来完成,也可以只是常规的static
方法。我面前没有 C# IDE,因此下面的示例代码是用 VB 编写的,但应该相当容易翻译。
您可以为每个对齐创建几个快速方法:
Public Shared Function CreateLeftAlignedCell(ByVal text As String) As PdfPCell
Return New PdfPCell(New Phrase(text)) With {.HorizontalAlignment = PdfPCell.ALIGN_LEFT}
End Function
Public Shared Function CreateRightAlignedCell(ByVal text As String) As PdfPCell
Return New PdfPCell(New Phrase(text)) With {.HorizontalAlignment = PdfPCell.ALIGN_RIGHT}
End Function
Public Shared Function CreateCenterAlignedCell(ByVal text As String) As PdfPCell
Return New PdfPCell(New Phrase(text)) With {.HorizontalAlignment = PdfPCell.ALIGN_CENTER}
End Function
或者只是您必须传入已知常量之一的单个常量:
Public Shared Function CreatePdfPCell(ByVal text As String, ByVal align As Integer) As PdfPCell
Return New PdfPCell(New Phrase(text)) With {.HorizontalAlignment = align}
End Function
然后你可以执行以下操作:
Dim T As New PdfPTable(3)
T.AddCell(CreateCenterAlignedCell("Hello"))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)