Set Row Height in PDF Table in C#

When creating a PDF table using PdfTable, there is no direct API available in this class that allows to change the row height of the table. However, it is possible to change the row height through BeginRowLayout event.

Step 1: Create a new PDF document.

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

Step 2: Initialize an instance of PdfTable class.

PdfTable table = new PdfTable();

Step 3: Create a DataTable.

DataTable dataTable = new DataTable();
dataTable.Columns.Add("ID");
dataTable.Columns.Add("First Name");
dataTable.Columns.Add("Last Name");
dataTable.Columns.Add("Job Id");
dataTable.Rows.Add(new string[] { "102", "Lexa", "De Haan","AD_VP" });
dataTable.Rows.Add(new string[] { "103", "Alexander", "Hunoldsssss","IT_PROG" });
dataTable.Rows.Add(new string[] { "104", "Bruce", "Ernst", "IT_PROG" });
dataTable.Rows.Add(new string[] { "105", "John", "Chen", "FI_ACCOUNT" })

Step 4: Assign data table as data source to the table.

table.DataSource = dataTable;
table.Style.ShowHeader = true;

Step 5: Subscribe to event.

table.BeginRowLayout += Table_BeginRowLayout;

Step 6: Draw the table on the page and save the document.

table.Draw(page, new RectangleF(0,20,300,90)); 
doc.SaveToFile("Output.pdf");

Step 7: Set the row height in the BeginRowLayout event.

private static void Table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
{
    args.MinimalHeight = 15f;
}

Output:

How to Set Row Height in PDF Table in C#

Full Code:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Tables;
using System.Data;
using System.Drawing;


namespace SetRowHeight
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();

            PdfTable table = new PdfTable();
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("ID");
            dataTable.Columns.Add("First Name");
            dataTable.Columns.Add("Last Name");
            dataTable.Columns.Add("Job Id");
            dataTable.Rows.Add(new string[] { "102", "Lexa", "De Haan", "AD_VP" });
            dataTable.Rows.Add(new string[] { "103", "Alexander", "Hunoldsssss", "IT_PROG" });
            dataTable.Rows.Add(new string[] { "104", "Bruce", "Ernst", "IT_PROG" });
            dataTable.Rows.Add(new string[] { "105", "John", "Chen", "FI_ACCOUNT" });
            table.DataSource = dataTable;
            table.Style.ShowHeader = true;

            foreach (PdfColumn col in table.Columns)
            {
                col.StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
            }
            table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.Gray;

            table.BeginRowLayout += Table_BeginRowLayout;
            table.Draw(page, new RectangleF(0, 20, 300, 90));
            doc.SaveToFile("Output.pdf");


            System.Diagnostics.Process.Start("Output.pdf");

        }
        private static void Table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
        {
            args.MinimalHeight = 15f;
        }
    }
}