ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Change Row Height while exporting Windows Form DataGridView to PDF using iTextSharp in C# and VB.Net<p>Hi&nbsp;PSowmiya,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p>Using the below article i have created the example.</p> <h1 class="header"><a title="Export Windows Forms DataGridView to PDF using iTextSharp, C# and VB.Net'a" href="https://www.aspsnippets.com/Articles/Export-Windows-Forms-DataGridView-to-PDF-using-iTextSharp-C-and-VBNet.aspx">Export Windows Forms DataGridView to PDF using iTextSharp, C# and VB.Net</a></h1> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">public Form1() { InitializeComponent(); this.BindDataGridView(); } private void BindDataGridView() { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn(&#34;Id&#34;, typeof(int)), new DataColumn(&#34;Name&#34;, typeof(string)), new DataColumn(&#34;Country&#34;,typeof(string)) }); dt.Rows.Add(1, &#34;John Hammond&#34;, &#34;United States&#34;); dt.Rows.Add(2, &#34;Mudassar Khan&#34;, &#34;India&#34;); dt.Rows.Add(3, &#34;Suzanne Mathews&#34;, &#34;France&#34;); dt.Rows.Add(4, &#34;Robert Schidner&#34;, &#34;Russia&#34;); this.dataGridView1.DataSource = dt; } private void btnExportPdf_Click(object sender, EventArgs e) { //Creating iTextSharp Table from the DataTable data PdfPTable pdfTable = new PdfPTable(dataGridView1.ColumnCount); // Set each cell height of rows except Header. pdfTable.DefaultCell.FixedHeight = 20f; pdfTable.DefaultCell.Padding = 3; pdfTable.WidthPercentage = 90; pdfTable.HorizontalAlignment = Element.ALIGN_LEFT; pdfTable.DefaultCell.BorderWidth = 1; //Adding Header row foreach (DataGridViewColumn column in dataGridView1.Columns) { PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText)); // Set each cell height of Header. cell.FixedHeight = 20f; cell.BackgroundColor = new iTextSharp.text.Color(240, 240, 240); pdfTable.AddCell(cell); } //Adding DataRow foreach (DataGridViewRow row in dataGridView1.Rows) { foreach (DataGridViewCell cell in row.Cells) { pdfTable.AddCell(cell.Value.ToString()); } } //Exporting to PDF string folderPath = @&#34;C:\Desktop\&#34;; if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } using (FileStream stream = new FileStream(folderPath + &#34;DataGridViewExport.pdf&#34;, FileMode.Create)) { Document pdfDoc = new Document(PageSize.A5, 10f, 10f, 10f, 0f); PdfWriter.GetInstance(pdfDoc, stream); pdfDoc.Open(); pdfDoc.Add(pdfTable); pdfDoc.Close(); stream.Close(); } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Public Sub New() InitializeComponent() Me.BindDataGridView() End Sub Private Sub BindDataGridView() Dim dt As New DataTable() dt.Columns.AddRange(New DataColumn() {New DataColumn(&#34;Id&#34;, GetType(Integer)), _ New DataColumn(&#34;Name&#34;, GetType(String)), _ New DataColumn(&#34;Country&#34;, GetType(String))}) dt.Rows.Add(1, &#34;John Hammond&#34;, &#34;United States&#34;) dt.Rows.Add(2, &#34;Mudassar Khan&#34;, &#34;India&#34;) dt.Rows.Add(3, &#34;Suzanne Mathews&#34;, &#34;France&#34;) dt.Rows.Add(4, &#34;Robert Schidner&#34;, &#34;Russia&#34;) Me.dataGridView1.DataSource = dt End Sub Private Sub btnExportPDF_Click(sender As System.Object, e As System.EventArgs) Handles btnExportPDF.Click &#39;Creating iTextSharp Table from the DataTable data Dim pdfTable As New PdfPTable(dataGridView1.ColumnCount) &#39; Set each cell height of rows except Header. pdfTable.DefaultCell.FixedHeight = 20.0F pdfTable.DefaultCell.Padding = 3 pdfTable.WidthPercentage = 90 pdfTable.HorizontalAlignment = Element.ALIGN_LEFT pdfTable.DefaultCell.BorderWidth = 1 &#39;Adding Header row For Each column As DataGridViewColumn In dataGridView1.Columns Dim cell As New PdfPCell(New Phrase(column.HeaderText)) &#39; Set each cell height of Header. cell.FixedHeight = 20.0F cell.BackgroundColor = New iTextSharp.text.Color(240, 240, 240) pdfTable.AddCell(cell) Next &#39;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 &#39;Exporting to PDF Dim folderPath As String = &#34;C:\Desktop\&#34; If Not Directory.Exists(folderPath) Then Directory.CreateDirectory(folderPath) End If Using stream As New FileStream(folderPath &amp; &#34;DataGridViewExport.pdf&#34;, 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 End Sub</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/pJjJkHh.jpg" alt="" width="472" height="237" /></span></strong></p>https://www.aspforums.net:443/Threads/206502/Change-Row-Height-while-exporting-Windows-Form-DataGridView-to-PDF-using-iTextSharp-in-C-and-VBNet/https://www.aspforums.net:443/Threads/206502/Change-Row-Height-while-exporting-Windows-Form-DataGridView-to-PDF-using-iTextSharp-in-C-and-VBNet/Fri, 02 Nov 2018 04:44:28 GMT