Data Tables to PDF in C#.

Welcome back, dear readers, to another informative blog from Dotnet Expert! Today, we're diving into the realm of exporting Data Tables to PDF in C#. As developers, we often deal with data in various formats, and being able to seamlessly convert Data Tables to PDFs can prove immensely valuable. In this guide, we'll take you through a step-by-step process, enabling you to become a pro at this essential task.


Why Export Data Tables to PDF?

Before we delve into the technical details, let's discuss the significance of exporting Data Tables to PDF format. PDFs provide a universally accepted and visually consistent way to share information. Exporting Data Tables to PDFs is particularly useful when you need to present tabular data to stakeholders, clients, or colleagues, ensuring that the layout and formatting remain intact.


Step 1: Set Up Your Project

First, create a new C# project or open an existing one in your preferred Integrated Development Environment (IDE). Ensure you have the necessary libraries installed. For PDF generation, we'll use a popular library like iTextSharp, which provides powerful capabilities for creating PDF documents programmatically.


Step 2: Add References

Include the iTextSharp library in your project. You can either download it manually or install it via NuGet Package Manager. To install via NuGet, open the Package Manager Console and enter the following command:

bash

Copy code

Install-Package iTextSharp


Step 3: Create a Method for PDF Export

Next, create a method that accepts a Data Table as input and generates a PDF document. Within this method, you'll use iTextSharp to create the PDF content, layout, and formatting.

CSharp


Copy code

using System;

using System.Data;

using System.IO;

using iTextSharp.text;

using iTextSharp.text.pdf;

namespace DotnetExpert

{

public class PDFExporter

{

public void ExportDataTableToPDF(DataTable dataTable, string outputPath)

{

// Create a new PDF document

Document doc = new Document();

PdfWriter.GetInstance(doc, new FileStream(outputPath, FileMode.Create));

doc.Open();

// Add a table to the document

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

pdfTable.DefaultCell.Padding = 3;

pdfTable.WidthPercentage = 100;

pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;

// Add table headers

foreach (DataColumn column in dataTable.Columns)

{

PdfPCell cell = new PdfPCell(new Phrase(column.ColumnName));

pdfTable.AddCell(cell);

}

// Add table rows

foreach (DataRow row in dataTable.Rows)

{

foreach (var item in row.ItemArray)

{

pdfTable.AddCell(item.ToString());

}

}

// Add the table to the document

doc.Add(pdfTable);

doc.Close();

}

}

}


Step 4: Utilize the Export Method

Now that you have your export method in place, you can use it in your application whenever you need to export a Data Table to a PDF. Instantiate the PDFExporter class, call the ExportDataTableToPDF method, and provide the Data Table and desired output path as parameters.

CSharp

Copy code

using System;

using System.Data;

namespace DotnetExpert

{

class Program

{

static void Main(string[] args)

{

DataTable dataTable = GetSampleDataTable(); // Replace with your Data Table retrieval logic

string outputPath = "output.pdf";

PDFExporter pdfExporter = new PDFExporter();

pdfExporter.ExportDataTableToPDF(dataTable, outputPath);

Console.WriteLine("PDF export complete!");

}

static DataTable GetSampleDataTable()

{

// Create and populate a sample Data Table

DataTable dataTable = new DataTable();

dataTable.Columns.Add("ID", typeof(int));

dataTable.Columns.Add("Name", typeof(string));

dataTable.Columns.Add("Age", typeof(int));

dataTable.Rows.Add(1, "John Doe", 30);

dataTable.Rows.Add(2, "Jane Smith", 28);

return dataTable;

}

}

}


Congratulations! You've now mastered the art of exporting Data Tables to PDFs in C#. With the power of the iTextSharp library and your newfound knowledge, you can effortlessly transform tabular data into professional PDF documents. This skill will undoubtedly elevate your ability to communicate data effectively with stakeholders and clients.


Stay tuned for more Dotnet Expert blogs that empower you with in-depth technical insights and practical coding solutions.

Comments 0

contact.webp

SCHEDULE MEETING

Schedule A Custom 20 Min Consultation

Contact us today to schedule a free, 20-minute call to learn how DotNet Expert Solutions can help you revolutionize the way your company conducts business.

Schedule Meeting paperplane.webp