【发布时间】:2011-10-17 22:13:43
【问题描述】:
我正在创建一个表格,其中每一列都有自己的对齐方式,如下所示。我如何在列级别而不是在单元格级别完成它?
【问题讨论】:
标签: c# alignment itext tabular pdfptable
我正在创建一个表格,其中每一列都有自己的对齐方式,如下所示。我如何在列级别而不是在单元格级别完成它?
【问题讨论】:
标签: c# alignment itext tabular pdfptable
iText 和 iTextSharp 不支持列样式和格式。做到这一点的唯一方法就是像您目前所做的那样,逐个单元格地进行。
编辑
最简单的解决方法是创建设置常用属性的辅助方法。这些可以通过扩展方法或只是常规的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"))
【讨论】: