Exporting Data From DataTable To PDF

Sometimes, we are in a situation where we have to export data as a PDF.  We get data from the database and want it to be exported as a PDF file.

We will calculate the number of columns in the DataTable to make the columns of the PDF document. We will use iTextSharp to convert the DataTable data into PDF format. We will use the iTextSharp text font property to design our document. To convert the data to PDF, first we will create the headers of the document from Column Names and then we will fill in the actual data to the PDF document.

This LINQ Query will get the number of columns from the DataTable.

  1. string[] columnNames = (from dc in dataTable.Columns.Cast<DataColumn>()  
  2.                                         select dc.ColumnName).ToArray();   
For Header and Footer changes, we will use the following query.
  1. HeaderFooter header = new HeaderFooter(new Phrase(Name), false);  

Below is the C# example to convert DataTable to PDF and export it.

  1. private void GeneratePDF(DataTable dataTable, string Name)  
  2.       {  
  3.           try  
  4.           {  
  5.               string[] columnNames = (from dc in dataTable.Columns.Cast<DataColumn>()  
  6.                                       select dc.ColumnName).ToArray();  
  7.               int Cell = 0;  
  8.               int count = columnNames.Length;  
  9.               object[] array = new object[count];  
  10.   
  11.               dataTable.Rows.Add(array);  
  12.   
  13.               Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);  
  14.               System.IO.MemoryStream mStream = new System.IO.MemoryStream();  
  15.               PdfWriter writer = PdfWriter.GetInstance(pdfDoc, mStream);  
  16.               int cols = dataTable.Columns.Count;  
  17.               int rows = dataTable.Rows.Count;  
  18.                
  19.   
  20.               HeaderFooter header = new HeaderFooter(new Phrase(Name), false);  
  21.   
  22.               // Remove the border that is set by default  
  23.               header.Border = iTextSharp.text.Rectangle.TITLE;  
  24.               // Align the text: 0 is left, 1 center and 2 right.  
  25.               header.Alignment = Element.ALIGN_CENTER;  
  26.               pdfDoc.Header = header;  
  27.               // Header.  
  28.               pdfDoc.Open();  
  29.               iTextSharp.text.Table pdfTable = new iTextSharp.text.Table(cols, rows);  
  30.               pdfTable.BorderWidth = 1; pdfTable.Width = 100;  
  31.               pdfTable.Padding = 1; pdfTable.Spacing = 4;  
  32.   
  33.               //creating table headers  
  34.               for (int i = 0; i < cols; i++)  
  35.               {  
  36.                   Cell cellCols = new Cell();  
  37.                   Chunk chunkCols = new Chunk();  
  38.                   cellCols.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#548B54"));  
  39.                   iTextSharp.text.Font ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 14, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.WHITE);  
  40.                    
  41.                   chunkCols = new Chunk(dataTable.Columns[i].ColumnName, ColFont);  
  42.                  
  43.                   cellCols.Add(chunkCols);  
  44.                   pdfTable.AddCell(cellCols);  
  45.               }  
  46.               //creating table data (actual result)   
  47.   
  48.               for (int k = 0; k < rows; k++)  
  49.               {  
  50.                   for (int j = 0; j < cols; j++)  
  51.                   {  
  52.                       Cell cellRows = new Cell();  
  53.                       if (k % 2 == 0)  
  54.                       {  
  55.                           cellRows.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#cccccc")); ;  
  56.                       }  
  57.                       else { cellRows.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#ffffff")); }  
  58.                       iTextSharp.text.Font RowFont = FontFactory.GetFont(FontFactory.HELVETICA, 12);  
  59.                       Chunk chunkRows = new Chunk(dataTable.Rows[k][j].ToString(), RowFont);  
  60.                       cellRows.Add(chunkRows);  
  61.   
  62.                       pdfTable.AddCell(cellRows);  
  63.                   }  
  64.               }  
  65.   
  66.               pdfDoc.Add(pdfTable);  
  67.               pdfDoc.Close();  
  68.               Response.ContentType = "application/octet-stream";  
  69.               Response.AddHeader("Content-Disposition""attachment; filename=" + Name + "_" + DateTime.Now.ToString() + ".pdf");  
  70.               Response.Clear();  
  71.               Response.BinaryWrite(mStream.ToArray());  
  72.               Response.End();  
  73.   
  74.           }  
  75.           catch (Exception ex)  
  76.           {  
  77.                
  78.           }  
  79.       }   

We will have to pass the DataTable and FileName to this function.

  1. GeneratePDF(DataTable dataTable, string Name);

Next Recommended Reading Sorting Data in C# DataTable