1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
'Creating iTextSharp Table from the DataTable data
Dim pdfTable As New PdfPTable(DataGridView1.ColumnCount)
pdfTable.DefaultCell.Padding = 3
pdfTable.WidthPercentage = 30
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
pdfTable.DefaultCell.BorderWidth = 1
'Adding Header row
For Each column As DataGridViewColumn In DataGridView1.Columns
Dim cell As New PdfPCell(New Phrase(column.HeaderText))
' cell.BackgroundColor = New iTextSharp.text.Color(240, 240, 240)
pdfTable.AddCell(cell)
Next
'Adding DataRow
For Each row As DataGridViewRow In DataGridView1.Rows
For Each cell As DataGridViewCell In row.Cells
pdfTable.AddCell(cell.Value.ToString())
Next
Next
'Exporting to PDF
Dim folderPath As String = "C:\PDFs\"
If Not Directory.Exists(folderPath) Then
Directory.CreateDirectory(folderPath)
End If
Using stream As New FileStream(folderPath & "DataGridViewExport.pdf", FileMode.Create)
Dim pdfDoc As New Document(PageSize.A2, 10.0F, 10.0F, 10.0F, 0.0F)
PdfWriter.GetInstance(pdfDoc, stream)
pdfDoc.Open()
pdfDoc.Add(pdfTable)
pdfDoc.Close()
stream.Close()
End Using |
Partager