I have been using ITextSharp to replicate the information on a webpage so that the information on the page can be printed in PDF format.
I generate my table by using the following code.
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
How would I go about replicating this table in ITextSharp using Visual Basic. All of the solutions I have looked at so far have been in C# but I cant figure out to do this in VB
any advice would be appreciated.
Below is the most basic usage of iTextSharp in VB.Net. I'm not binding it to any data model, just two loops for rows and columns since it seems like you've got that part down. See the code comments for more details.
''//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