我正在尝试创建ItextSharp
表
表格格式让我发疯了
这是我到目前为止非常简单的代码
Dim objTable As PdfPTable = New PdfPTable(3)
objTable.AddCell("1")
objTable.AddCell("2")
objTable.AddCell("3")
Dim xp As Phrase = New Phrase("1.5 A")
Dim x As PdfPCell = New PdfPCell(xp)
x.Colspan = 1.5
objTable.AddCell(x)
Dim yp As Phrase = New Phrase("1.5 B")
Dim y As PdfPCell = New PdfPCell(yp)
y.Colspan = 1.5
objTable.AddCell(y)
这会生成下表
问题 什么是最好的方法 - 如果有的话 - 使第二行具有相等的列
让他们像这样
在中间?
答案 0 :(得分:1)
转动Option Strict On
您应该会看到第一个问题,Colspan
仅使用整数。
我能想到的唯一方法是将其拆分为更多列,就像我们以前在HTML表格中所做的那样。有两种基本方法,或者使用列数的最小公倍数(6),或者将中心列分成两个较小的列。我个人更喜欢第一种方法,因为我认为它更清洁。
''//Create a 6 column table with each column have about 16.666667%
Dim T1 As New PdfPTable(6)
''//For three across we span 2
T1.AddCell(New PdfPCell(New Phrase("1")) With {.Colspan = 2})
T1.AddCell(New PdfPCell(New Phrase("2")) With {.Colspan = 2})
T1.AddCell(New PdfPCell(New Phrase("3")) With {.Colspan = 2})
''//For two across we span 3
T1.AddCell(New PdfPCell(New Phrase("1.5A")) With {.Colspan = 3})
T1.AddCell(New PdfPCell(New Phrase("1.5B")) With {.Colspan = 3})
Doc.Add(T1)
''//Create a 4 column table with the center columns have half the width of the outer columns
Dim T2 As New PdfPTable({2, 1, 1, 2})
''//Add a normal cell
T2.AddCell(New PdfPCell(New Phrase("1")))
''//Add a cell that spans the two "smaller" columns
T2.AddCell(New PdfPCell(New Phrase("2")) With {.Colspan = 2})
''//Add a normal cell
T2.AddCell(New PdfPCell(New Phrase("3")))
''//In this row we need a full normal column plus one of the half columns
T2.AddCell(New PdfPCell(New Phrase("1.5A")) With {.Colspan = 2})
T2.AddCell(New PdfPCell(New Phrase("1.5B")) With {.Colspan = 2})
Doc.Add(T2)