ITextSharp 将现有 table 插入 pdf 文档
ITextSharp insert existing table into pdf document
我一直在使用ITextSharp复制网页上的信息,以便将网页上的信息打印成PDF格式。
我使用以下代码生成 table。
Protected Sub GenerateTable(noOfRows As Integer, reader As SqlClient.SqlDataReader)
Dim table As Table
Dim row As TableRow
Dim cell As TableCell
Dim lbl As Label
Dim lblVolume As Label
Dim lblUnitPrice As Label
Dim lblTotalPrice As Label
table = VolumeTable
table.ID = "VolumeTable"
'Page.Form.Controls.Add(table)
For i As Integer = 1 To noOfRows Step 1
row = New TableRow()
For j As Integer = 0 To 5 Step 1
cell = New TableCell()
If j = 1 Then
lblVolume = New Label()
lblVolume.ID = "LabelRow_" & i & "Col_" & j
cell.Controls.Add(lblVolume)
lblVolume.Text = reader.GetValue(2)
ElseIf j = 2 Then
lblUnitPrice = New Label()
lblUnitPrice.ID = "UnitLabel" & i
lblUnitPrice.Text = "Unit Price: "
cell.Controls.Add(lblUnitPrice)
ElseIf j = 3 Then
lblUnitPrice = New Label()
lblUnitPrice.ID = "LabelRow_" & i & "Col_" & j
cell.Controls.Add(lblUnitPrice)
lblUnitPrice.Text = reader.GetValue(5)
ElseIf j = 4 Then
lblUnitPrice = New Label()
lblUnitPrice.ID = "TotalPrice" & i
lblUnitPrice.Text = "Total Price: "
cell.Controls.Add(lblUnitPrice)
ElseIf j = 5 Then
lblTotalPrice = New Label()
lblTotalPrice.ID = "LabelRow_" & i & "Col_" & j
cell.Controls.Add(lblTotalPrice)
lblTotalPrice.Text = reader.GetValue(6)
ElseIf j = 0 Then
lbl = New Label()
lbl.ID = "Label" & i
lbl.Text = "Volume " & i
cell.Controls.Add(lbl)
End If
row.Cells.Add(cell)
Next
table.Rows.Add(row)
reader.Read()
Next
'SetPreviousTableData(noOfRows)
ViewState("RowsCount") = noOfRows
Session("RowsCount") = noOfRows
End Sub
我将如何使用 Visual Basic 在 ITextSharp 中复制这个 table。到目前为止我看过的所有解决方案都是在 C# 中,但我无法在 VB
中做到这一点
如有任何建议,我们将不胜感激。
下面是VB.Net中iTextSharp最基本的用法。我没有将它绑定到任何数据模型,只是行和列的两个循环,因为看起来你已经完成了那部分。请参阅代码注释以获取更多详细信息。
''//Filename that we're going to write to
Dim FileName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf")
''//Create a Stream to write to. This could also be a MemoryStream or anything else that inherits from System.IO.Stream
Using FS As New FileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.None)
''//Create an abstract PDF document
Using Doc As New Document()
''//Create a PdfWriter that binds the abstraction to the stream
Using Writer = PdfWriter.GetInstance(Doc, FS)
''//Open the document to allow writing
Doc.Open()
''//Create a Pdf table with 4 columns
Dim table As New PdfPTable(4)
''//Loop through 10 rows
For RowNumber = 1 To 10
''//Loop through 4 columns
For ColumnNumber = 1 To 4
''//Write some text to the table
table.AddCell(New Paragraph(String.Format("Hello from {0}x{1}", RowNumber, ColumnNumber)))
Next
Next
''//Add the table to the document
Doc.Add(table)
''//Close the document to disable writing and flush buffers
Doc.Close()
End Using
End Using
End Using
我一直在使用ITextSharp复制网页上的信息,以便将网页上的信息打印成PDF格式。
我使用以下代码生成 table。
Protected Sub GenerateTable(noOfRows As Integer, reader As SqlClient.SqlDataReader)
Dim table As Table
Dim row As TableRow
Dim cell As TableCell
Dim lbl As Label
Dim lblVolume As Label
Dim lblUnitPrice As Label
Dim lblTotalPrice As Label
table = VolumeTable
table.ID = "VolumeTable"
'Page.Form.Controls.Add(table)
For i As Integer = 1 To noOfRows Step 1
row = New TableRow()
For j As Integer = 0 To 5 Step 1
cell = New TableCell()
If j = 1 Then
lblVolume = New Label()
lblVolume.ID = "LabelRow_" & i & "Col_" & j
cell.Controls.Add(lblVolume)
lblVolume.Text = reader.GetValue(2)
ElseIf j = 2 Then
lblUnitPrice = New Label()
lblUnitPrice.ID = "UnitLabel" & i
lblUnitPrice.Text = "Unit Price: "
cell.Controls.Add(lblUnitPrice)
ElseIf j = 3 Then
lblUnitPrice = New Label()
lblUnitPrice.ID = "LabelRow_" & i & "Col_" & j
cell.Controls.Add(lblUnitPrice)
lblUnitPrice.Text = reader.GetValue(5)
ElseIf j = 4 Then
lblUnitPrice = New Label()
lblUnitPrice.ID = "TotalPrice" & i
lblUnitPrice.Text = "Total Price: "
cell.Controls.Add(lblUnitPrice)
ElseIf j = 5 Then
lblTotalPrice = New Label()
lblTotalPrice.ID = "LabelRow_" & i & "Col_" & j
cell.Controls.Add(lblTotalPrice)
lblTotalPrice.Text = reader.GetValue(6)
ElseIf j = 0 Then
lbl = New Label()
lbl.ID = "Label" & i
lbl.Text = "Volume " & i
cell.Controls.Add(lbl)
End If
row.Cells.Add(cell)
Next
table.Rows.Add(row)
reader.Read()
Next
'SetPreviousTableData(noOfRows)
ViewState("RowsCount") = noOfRows
Session("RowsCount") = noOfRows
End Sub
我将如何使用 Visual Basic 在 ITextSharp 中复制这个 table。到目前为止我看过的所有解决方案都是在 C# 中,但我无法在 VB
中做到这一点如有任何建议,我们将不胜感激。
下面是VB.Net中iTextSharp最基本的用法。我没有将它绑定到任何数据模型,只是行和列的两个循环,因为看起来你已经完成了那部分。请参阅代码注释以获取更多详细信息。
''//Filename that we're going to write to
Dim FileName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf")
''//Create a Stream to write to. This could also be a MemoryStream or anything else that inherits from System.IO.Stream
Using FS As New FileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.None)
''//Create an abstract PDF document
Using Doc As New Document()
''//Create a PdfWriter that binds the abstraction to the stream
Using Writer = PdfWriter.GetInstance(Doc, FS)
''//Open the document to allow writing
Doc.Open()
''//Create a Pdf table with 4 columns
Dim table As New PdfPTable(4)
''//Loop through 10 rows
For RowNumber = 1 To 10
''//Loop through 4 columns
For ColumnNumber = 1 To 4
''//Write some text to the table
table.AddCell(New Paragraph(String.Format("Hello from {0}x{1}", RowNumber, ColumnNumber)))
Next
Next
''//Add the table to the document
Doc.Add(table)
''//Close the document to disable writing and flush buffers
Doc.Close()
End Using
End Using
End Using