Change Row Height while exporting Windows Form DataGridView to PDF using iTextSharp in C# and VB.Net

Last Reply 4 months ago By dharmendr

Posted 4 months ago

Hi Sir,

I want to change the height of PdfTable row in ITextSharp .How to achieve it?

Posted 4 months ago

Hi PSowmiya,

Check this example. Now please take its reference and correct your code.

Using the below article i have created the example.

Export Windows Forms DataGridView to PDF using iTextSharp, C# and VB.Net

Code

C#

public Form1()
{
    InitializeComponent();
    this.BindDataGridView();
}

private void BindDataGridView()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
    new DataColumn("Name", typeof(string)),
    new DataColumn("Country",typeof(string)) });
    dt.Rows.Add(1, "John Hammond", "United States");
    dt.Rows.Add(2, "Mudassar Khan", "India");
    dt.Rows.Add(3, "Suzanne Mathews", "France");
    dt.Rows.Add(4, "Robert Schidner", "Russia");
    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 = @"C:\Desktop\";
    if (!Directory.Exists(folderPath))
    {
        Directory.CreateDirectory(folderPath);
    }
    using (FileStream stream = new FileStream(folderPath + "DataGridViewExport.pdf", 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();
    }
}

VB.Net

Public Sub New()
    InitializeComponent()
    Me.BindDataGridView()
End Sub

Private Sub BindDataGridView()
    Dim dt As New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("Id", GetType(Integer)), _
                                           New DataColumn("Name", GetType(String)), _
                                           New DataColumn("Country", GetType(String))})
    dt.Rows.Add(1, "John Hammond", "United States")
    dt.Rows.Add(2, "Mudassar Khan", "India")
    dt.Rows.Add(3, "Suzanne Mathews", "France")
    dt.Rows.Add(4, "Robert Schidner", "Russia")
    Me.dataGridView1.DataSource = dt
End Sub

Private Sub btnExportPDF_Click(sender As System.Object, e As System.EventArgs) Handles btnExportPDF.Click
    'Creating iTextSharp Table from the DataTable data
    Dim pdfTable As New PdfPTable(dataGridView1.ColumnCount)
    ' 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

    'Adding Header row
    For Each column As DataGridViewColumn In dataGridView1.Columns
        Dim cell As New PdfPCell(New Phrase(column.HeaderText))
        ' Set each cell height of Header.
        cell.FixedHeight = 20.0F
        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:\Desktop\"
    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
End Sub

Screenshot