A
ai1231
Guest
Hi!
I am trying to export datagridview1 to pdf using itextsharp and I am able to do it with this code.
The problem is that the table is being exported to the top of the pdf page instead I want it to be in the middle or an specific position.How I can do this?
Dim pdfTable As PdfPTable = New PdfPTable(DataGridView1.ColumnCount)
pdfTable.DefaultCell.Padding = 3
pdfTable.WidthPercentage = 100
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
pdfTable.DefaultCell.BorderWidth = 1
For Each column As DataGridViewColumn In DataGridView1.Columns
Dim cell As PdfPCell = New PdfPCell(New Phrase(column.HeaderText.Replace("_", " ").Replace("ne perq", "%")))
pdfTable.AddCell(cell)
Next
Dim row As Integer = DataGridView1.Rows.Count
Dim cell2 As Integer = DataGridView1.Rows(1).Cells.Count
For i As Integer = 0 To row - 1 - 1
For j As Integer = 0 To cell2 - 1
If DataGridView1.Rows(i).Cells(j).Value Is Nothing Then
DataGridView1.Rows(i).Cells(j).Value = "null"
End If
pdfTable.AddCell(DataGridView1.Rows(i).Cells(j).Value.ToString())
Next
Next
Dim folderPath As String = "D:\Log\"
If Not Directory.Exists(folderPath) Then
Directory.CreateDirectory(folderPath)
End If
Using stream As FileStream = New FileStream(folderPath & "DataGridViewExport.pdf", FileMode.Create)
Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0F)
PdfWriter.GetInstance(pdfDoc, stream)
pdfDoc.Open()
pdfDoc.Add(pdfTable)
pdfDoc.Close()
stream.Close()
End Using
MessageBox.Show("Done")
Continue reading...
I am trying to export datagridview1 to pdf using itextsharp and I am able to do it with this code.
The problem is that the table is being exported to the top of the pdf page instead I want it to be in the middle or an specific position.How I can do this?
Dim pdfTable As PdfPTable = New PdfPTable(DataGridView1.ColumnCount)
pdfTable.DefaultCell.Padding = 3
pdfTable.WidthPercentage = 100
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
pdfTable.DefaultCell.BorderWidth = 1
For Each column As DataGridViewColumn In DataGridView1.Columns
Dim cell As PdfPCell = New PdfPCell(New Phrase(column.HeaderText.Replace("_", " ").Replace("ne perq", "%")))
pdfTable.AddCell(cell)
Next
Dim row As Integer = DataGridView1.Rows.Count
Dim cell2 As Integer = DataGridView1.Rows(1).Cells.Count
For i As Integer = 0 To row - 1 - 1
For j As Integer = 0 To cell2 - 1
If DataGridView1.Rows(i).Cells(j).Value Is Nothing Then
DataGridView1.Rows(i).Cells(j).Value = "null"
End If
pdfTable.AddCell(DataGridView1.Rows(i).Cells(j).Value.ToString())
Next
Next
Dim folderPath As String = "D:\Log\"
If Not Directory.Exists(folderPath) Then
Directory.CreateDirectory(folderPath)
End If
Using stream As FileStream = New FileStream(folderPath & "DataGridViewExport.pdf", FileMode.Create)
Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0F)
PdfWriter.GetInstance(pdfDoc, stream)
pdfDoc.Open()
pdfDoc.Add(pdfTable)
pdfDoc.Close()
stream.Close()
End Using
MessageBox.Show("Done")
Continue reading...