Export SQL Data in to PDF Format

  1. using System;  
  2. using System.Windows.Forms;  
  3. using System.IO;  
  4. using System.Data;  
  5. using System.Reflection;  
  6. using iTextSharp.text.pdf;  
  7. using iTextSharp.text;  
  8. using System.Net;  
  9.   
  10. namespace Export_DataTable_PDF  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.             this.BindDataGridView();  
  18.         }  
  19.   
  20.         private void BindDataGridView()  
  21.         {  
  22.             DataTable dt = new DataTable();  
  23.             dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"typeof(int)),  
  24.             new DataColumn("Name"typeof(string)),  
  25.             new DataColumn("Country",typeof(string)) });  
  26.             dt.Rows.Add(1, "puneet jain""MP");  
  27.             dt.Rows.Add(2, "pintoo""India up");  
  28.             dt.Rows.Add(3, "Suzanne Mathews""France");  
  29.             dt.Rows.Add(4, "Robert Schidner""Russia");  
  30.             this.dataGridView1.DataSource = dt;  
  31.         }  
  32.   
  33.         private void btnExportPdf_Click(object sender, EventArgs e)  
  34.         {  
  35.             //Creating iTextSharp Table from the DataTable data  
  36.             PdfPTable pdfTable = new PdfPTable(dataGridView1.ColumnCount);  
  37.             pdfTable.DefaultCell.Padding = 3;  
  38.             pdfTable.WidthPercentage = 30;  
  39.             pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;  
  40.             pdfTable.DefaultCell.BorderWidth = 1;  
  41.   
  42.             //Adding Header row  
  43.             foreach (DataGridViewColumn column in dataGridView1.Columns)  
  44.             {  
  45.                 PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText));  
  46.                 cell.BackgroundColor = new iTextSharp.text.Color(240, 240, 240);  
  47.                 pdfTable.AddCell(cell);  
  48.             }  
  49.   
  50.             //Adding DataRow  
  51.             foreach (DataGridViewRow row in dataGridView1.Rows)  
  52.             {  
  53.                 foreach (DataGridViewCell cell in row.Cells)  
  54.                 {  
  55.                     pdfTable.AddCell(cell.Value.ToString());  
  56.                 }  
  57.             }  
  58.   
  59.             //Exporting to PDF  
  60.             string folderPath = "D:\\NACH_Credit_Presentation20_14\\";  
  61.             if (!Directory.Exists(folderPath))  
  62.             {  
  63.                 Directory.CreateDirectory(folderPath);  
  64.             }  
  65.             using (FileStream stream = new FileStream(folderPath + "DataGridViewExport.pdf", FileMode.Create))  
  66.             {  
  67.                 Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);  
  68.                 PdfWriter.GetInstance(pdfDoc, stream);  
  69.                 pdfDoc.Open();  
  70.                 pdfDoc.Add(pdfTable);  
  71.                 pdfDoc.Close();  
  72.                 stream.Close();  
  73.             }  
  74.         }  
  75.   
  76.         private void button1_Click(object sender, EventArgs e)  
  77.         {  
  78.   
  79.   
  80.             {  
  81.   
  82.                 OpenFileDialog dlg = new OpenFileDialog();  
  83.                 // set file filter of dialog   
  84.                 dlg.Filter = "pdf files (*.pdf) |*.pdf;";  
  85.                 dlg.ShowDialog();  
  86.                 if (dlg.FileName != null)  
  87.                 {  
  88.                       
  89.                   // axAcroPDF1.LoadFile(dlg.FileName);  
  90.                    pictureBox1.Load(dlg.FileName);  
  91.                       
  92.                    
  93.                 }  
  94.   
  95.             }  
  96.         }  
  97.   
  98.         private void Form1_Load(object sender, EventArgs e)  
  99.         {  
  100.   
  101.   
  102.               
  103.         }  
  104.     }  
  105. }  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Windows.Forms;  
  5.   
  6. namespace Export_DataTable_PDF  
  7. {  
  8.     static class Program  
  9.     {  
  10.         /// <summary>  
  11.         /// The main entry point for the application.  
  12.         /// </summary>  
  13.         [STAThread]  
  14.         static void Main()  
  15.         {  
  16.             Application.EnableVisualStyles();  
  17.             Application.SetCompatibleTextRenderingDefault(false);  
  18.             Application.Run(new Form1());  
  19.         }  
  20.     }  
  21. }