ItextSharp:使用循环将单元格添加到 table
ItextSharp : Adding cells to table using loop
由于我是 ItextSharp 的新手,我正在尝试更改关于如何将单元格添加到 PdfPTable 的现有功能。目前单元格是按行添加的,我希望它们在列的帮助下按列添加一个循环。
有两个列表,第一个列表填充第一列,第二个列表填充整个第二列。如果列表的行数不相等,我必须针对这些列添加空单元格。
我尝试为此在网络上进行一些研究,但找不到合适的解决方案。
有人可以提供一个例子来实现这个吗?
我不会回答任何特定于 iText 的问题,因为这更像是一个一般的编程问题。基本思想是您需要找到两个列表的最大计数,从零循环到最大值,然后检查每个列表以查看该值是否在范围内。如果它在范围内,则使用该值,如果不在范围内,则创建空单元格。下面是一个非常简单的实现:
''//Two sample lists
Dim ListA As New List(Of String) From {"A", "B", "C"}
Dim ListB As New List(Of String) From {"Apple", "Bat"}
''//Find the maximum count of the two lists
Dim X = Math.Max(ListA.Count, ListB.Count)
''//Index notations are zero-based so loop from zero to one
''//less than the total
For I = 0 To (X - 1)
If I >= ListA.Count Then
''//Our index is past the limit of the first column, add empty cell here
Console.WriteLine("<A>")
Else
''//Our index is within the limits of the first column
Console.WriteLine(ListA(I))
End If
If I >= ListB.Count Then
''//Our index is past the limit of the second column, add empty cell here
Console.WriteLine("<B>")
Else
''//Our index is within the limits of the first column
Console.WriteLine(ListB(I))
End If
Next
编辑
如果您有不止两列,您可以将其推断为如下所示:
Dim ListA As New List(Of String) From {"A", "B", "C"}
Dim ListB As New List(Of String) From {"Apple", "Bat"}
Dim ListC As New List(Of String) From {"Alice", "Bob", "Charlie", "Daniel"}
Dim AllLists As New List(Of IEnumerable(Of Object))
AllLists.Add(ListA)
AllLists.Add(ListB)
AllLists.Add(ListC)
Dim Z = AllLists.Max(Function(l) l.Count)
For I = 0 To (Z - 1)
For Each L In AllLists
If I >= L.Count Then
Console.WriteLine("<BLANK>")
Else
Console.WriteLine(L(I).ToString())
End If
Next
Next
由于我是 ItextSharp 的新手,我正在尝试更改关于如何将单元格添加到 PdfPTable 的现有功能。目前单元格是按行添加的,我希望它们在列的帮助下按列添加一个循环。
有两个列表,第一个列表填充第一列,第二个列表填充整个第二列。如果列表的行数不相等,我必须针对这些列添加空单元格。
我尝试为此在网络上进行一些研究,但找不到合适的解决方案。
有人可以提供一个例子来实现这个吗?
我不会回答任何特定于 iText 的问题,因为这更像是一个一般的编程问题。基本思想是您需要找到两个列表的最大计数,从零循环到最大值,然后检查每个列表以查看该值是否在范围内。如果它在范围内,则使用该值,如果不在范围内,则创建空单元格。下面是一个非常简单的实现:
''//Two sample lists
Dim ListA As New List(Of String) From {"A", "B", "C"}
Dim ListB As New List(Of String) From {"Apple", "Bat"}
''//Find the maximum count of the two lists
Dim X = Math.Max(ListA.Count, ListB.Count)
''//Index notations are zero-based so loop from zero to one
''//less than the total
For I = 0 To (X - 1)
If I >= ListA.Count Then
''//Our index is past the limit of the first column, add empty cell here
Console.WriteLine("<A>")
Else
''//Our index is within the limits of the first column
Console.WriteLine(ListA(I))
End If
If I >= ListB.Count Then
''//Our index is past the limit of the second column, add empty cell here
Console.WriteLine("<B>")
Else
''//Our index is within the limits of the first column
Console.WriteLine(ListB(I))
End If
Next
编辑 如果您有不止两列,您可以将其推断为如下所示:
Dim ListA As New List(Of String) From {"A", "B", "C"}
Dim ListB As New List(Of String) From {"Apple", "Bat"}
Dim ListC As New List(Of String) From {"Alice", "Bob", "Charlie", "Daniel"}
Dim AllLists As New List(Of IEnumerable(Of Object))
AllLists.Add(ListA)
AllLists.Add(ListB)
AllLists.Add(ListC)
Dim Z = AllLists.Max(Function(l) l.Count)
For I = 0 To (Z - 1)
For Each L In AllLists
If I >= L.Count Then
Console.WriteLine("<BLANK>")
Else
Console.WriteLine(L(I).ToString())
End If
Next
Next