ITextSharp将现有表插入pdf文档

时间:2015-07-22 11:55:17

标签: vb.net pdf itextsharp basic

我一直在使用ITextSharp复制网页上的信息,以便页面上的信息可以PDF格式打印。

我使用以下代码生成我的表。

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中复制此表。到目前为止,我所看到的所有解决方案都在C#中,但我无法在VB中做到这一点

任何建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

以下是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