【问题标题】:itextsharp colspan formatting issuesitextsharp colspan 格式问题
【发布时间】:2013-06-03 21:20:47
【问题描述】:

我试图创建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)

这会生成下表

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

让他们变成这样

就在中间?

【问题讨论】:

    标签: vb.net itext reporting


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多