Set Column Width while exporting Windows Form DataGridView to PDF using iTextSharp in VB.Net

Last Reply 7 months ago By pandeyism

Posted 7 months ago

My Gridview has 10 columns.So Column is not aligned properly while exporting from Datagridview To PDF using ITextSharp.How to resolve it?

'creating iTextSharp table from datagridview
Dim pdfTable As New PdfPTable(purchaseorderDGV.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 purchaseorderDGV.Columns
Dim cell As New PdfPCell(New Phrase(column.HeaderText))
pdfTable.AddCell(cell)
Next
'Adding Data row
For Each row As DataGridViewRow In purchaseorderDGV.Rows
For Each cell As DataGridViewCell In row.Cells
If cell.ColumnIndex <> 1 Then
pdfTable.AddCell(cell.Value.ToString)
Else
pdfTable.AddCell(Convert.ToDateTime(cell.Value.ToString))
End If
Next
Next
 
'Exporting to PDF
Dim folderPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "PODetails.pdf")
Using Stream As New FileStream(folderPath, 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
MessageBox.Show("PO is exported as PDF file successfully!!")

 

You are viewing reply posted by: pandeyism 7 months ago.
Posted 7 months ago

Hi PSowmiya,

Please increase the pdftable of width percentage.

PSowmiya says:
pdfTable.WidthPercentage = 30

 refer below line of coe.

pdfTable.WidthPercentage = 90;