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.
- string[] columnNames = (from dc in dataTable.Columns.Cast<DataColumn>()
- select dc.ColumnName).ToArray();
For Header and Footer changes, we will use the following query.
- HeaderFooter header = new HeaderFooter(new Phrase(Name), false);
Below is the C# example to convert DataTable to PDF and export it.
- private void GeneratePDF(DataTable dataTable, string Name)
- {
- try
- {
- string[] columnNames = (from dc in dataTable.Columns.Cast<DataColumn>()
- select dc.ColumnName).ToArray();
- int Cell = 0;
- int count = columnNames.Length;
- object[] array = new object[count];
-
- dataTable.Rows.Add(array);
-
- Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
- System.IO.MemoryStream mStream = new System.IO.MemoryStream();
- PdfWriter writer = PdfWriter.GetInstance(pdfDoc, mStream);
- int cols = dataTable.Columns.Count;
- int rows = dataTable.Rows.Count;
-
-
- HeaderFooter header = new HeaderFooter(new Phrase(Name), false);
-
-
- header.Border = iTextSharp.text.Rectangle.TITLE;
-
- header.Alignment = Element.ALIGN_CENTER;
- pdfDoc.Header = header;
-
- pdfDoc.Open();
- iTextSharp.text.Table pdfTable = new iTextSharp.text.Table(cols, rows);
- pdfTable.BorderWidth = 1; pdfTable.Width = 100;
- pdfTable.Padding = 1; pdfTable.Spacing = 4;
-
-
- for (int i = 0; i < cols; i++)
- {
- Cell cellCols = new Cell();
- Chunk chunkCols = new Chunk();
- cellCols.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#548B54"));
- iTextSharp.text.Font ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 14, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.WHITE);
-
- chunkCols = new Chunk(dataTable.Columns[i].ColumnName, ColFont);
-
- cellCols.Add(chunkCols);
- pdfTable.AddCell(cellCols);
- }
-
-
- for (int k = 0; k < rows; k++)
- {
- for (int j = 0; j < cols; j++)
- {
- Cell cellRows = new Cell();
- if (k % 2 == 0)
- {
- cellRows.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#cccccc")); ;
- }
- else { cellRows.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#ffffff")); }
- iTextSharp.text.Font RowFont = FontFactory.GetFont(FontFactory.HELVETICA, 12);
- Chunk chunkRows = new Chunk(dataTable.Rows[k][j].ToString(), RowFont);
- cellRows.Add(chunkRows);
-
- pdfTable.AddCell(cellRows);
- }
- }
-
- pdfDoc.Add(pdfTable);
- pdfDoc.Close();
- Response.ContentType = "application/octet-stream";
- Response.AddHeader("Content-Disposition", "attachment; filename=" + Name + "_" + DateTime.Now.ToString() + ".pdf");
- Response.Clear();
- Response.BinaryWrite(mStream.ToArray());
- Response.End();
-
- }
- catch (Exception ex)
- {
-
- }
- }
We will have to pass the DataTable and FileName to this function.
- GeneratePDF(DataTable dataTable, string Name);