itextsharp colspan格式问题

问题描述 投票:0回答:1

我正在尝试创建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)

这将生成下表

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9wSWpDci5wbmcifQ==” alt =“在此处输入图像描述”>

问题什么是最好的方法-如果有的话-使第二行具有相等的列

并使它们像这样

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9JcllMUS5wbmcifQ==” alt =“在此处输入图像描述”>就在中间吗?

vb.net itext reporting
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.