Exporting DataTable to PDF in C#

Introduction

Exporting data from a DataTable to PDF format is a common requirement in many applications. PDF (Portable Document Format) is widely used for document sharing and printing, making it an ideal choice for exporting tabular data. In this article, we will walk you through the process of exporting a DataTable to PDF using C#.

In C#, a DataTable is a data structure that stores data in rows and columns. It is often used to store data from a database or other data source. To export a DataTable to PDF, we can use the iTextSharp library. iTextSharp is a free and open-source library for creating and manipulating PDF documents.

This article has shown how to export a DataTable to PDF in C# using iTextSharp. The code snippet provided can be used as a starting point for your own projects.

Here are some additional things to consider when exporting a DataTable to PDF:

  • You can customize the appearance of the PDF document by setting the font, font size, margins, and other properties.
  • You can also add images, tables, and other content to the PDF document.

If you are exporting a large DataTable, you may want to break it up into multiple pages.

Step 1. Importing the Necessary Libraries

Before we begin, make sure you have the required libraries imported into your project:

using System;
using System.Data;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

Step  2. Creating the PDF Export Method

Next, create a method that takes the DataTable as input and generates the PDF document:

private void ExportDataTableToPDF(DataTable dataTable, string filePath)
{
    Document document = new Document();
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
    document.Open();

    PdfPTable pdfTable = new PdfPTable(dataTable.Columns.Count);
    pdfTable.WidthPercentage = 100;

    // Add table headers
    for (int i = 0; i < dataTable.Columns.Count; i++)
    {
        PdfPCell cell = new PdfPCell(new Phrase(dataTable.Columns[i].ColumnName));
        cell.BackgroundColor = BaseColor.LightGray;
        pdfTable.AddCell(cell);
    }

    // Add table rows
    for (int row = 0; row < dataTable.Rows.Count; row++)
    {
        for (int column = 0; column < dataTable.Columns.Count; column++)
        {
            pdfTable.AddCell(dataTable.Rows[row][column].ToString());
        }
    }

    document.Add(pdfTable);
    document.Close();
}

Step 3. Calling the Export Method

Finally, call the ExportDataTableToPDF method by passing the DataTable and the file path as arguments:

DataTable dataTable = GetDataTableFromDatabase(); // Retrieve DataTable from your data source
string filePath = "C:\\ExportedData.pdf"; // Provide the desired path for the PDF file
ExportDataTableToPDF(dataTable, filePath);

Conclusion

Exporting a DataTable to PDF in C# is made easy with the iTextSharp library. By following the steps outlined in this article, you can generate a professional-looking PDF document containing your tabular data. Remember to customize the design and layout of the PDF table according to your specific requirements.

In this article, we have covered the basic code snippets and steps required to export a DataTable to PDF. Feel free to explore additional features offered by iTextSharp to further enhance the output PDF document. Please note that iTextSharp is no longer actively maintained and has been replaced by iText 7. Consider migrating to iText 7 for future developments.