2015-07-22 4 views
0

Я использую ITextSharp для репликации информации на веб-странице, чтобы можно было напечатать информацию на странице в формате PDF.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 

Как бы я хотел воспроизвести эту таблицу в ITextSharp с помощью Visual Basic. Все решения, которые я рассматривал до сих пор, были на C#, но я не могу это сделать в VB

любые советы будут оценены.

ответ

0

Ниже приведено самое основное использование iTextSharp в VB.Net. Я не привязываю его к какой-либо модели данных, всего две петли для строк и столбцов, поскольку кажется, что у вас есть эта часть. Дополнительную информацию см. В комментариях к коду.

''//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 
+0

Спасибо Крис, это сработало. –

Смежные вопросы