Export DataGridView Data To PDF In C#

In this code example, we will learn how to export DataGridView data to a pdf file and save that in a folder using C# code.
 
In this program, first, we have to get connected to the database and fetch data from the DB and show it in the Data GridView like the below image.

 
Before we go to the coding section, we must add iTextSharp library reference to our project.

To do that, right-click on the project and select "Manage NuGet Package" menu. After that go to the "Browse" tab and search for iTextSharp and install it.

Project --> Manage NuGet Package --> Browse tab --> Search iTextSharp; then install.

Now, go to the page load event, fetch employee data and bind to the data GridView.
  1. private void FrmExport_Load(object sender, EventArgs e)  
  2. {  
  3.     SqlConnection sqlCon;  
  4.     string conString = null;  
  5.     string sqlQuery = null;  
  6.   
  7.     conString = "Data Source=.;Initial Catalog=DemoTest;Integrated Security=SSPI;";  
  8.     sqlCon = new SqlConnection(conString);  
  9.     sqlCon.Open();  
  10.     sqlQuery = "SELECT * FROM tblEmployee";  
  11.     SqlDataAdapter dscmd = new SqlDataAdapter(sqlQuery, sqlCon);  
  12.     DataTable dtData = new DataTable();  
  13.     dscmd.Fill(dtData);  
  14.     dataGridView1.DataSource = dtData;  

Then, on the buttonclick event handler, we have to create a PdfTable object and document, get data from DataGridView, and add rows and columns to the document.
  1. private void btnPdf_Click(object sender, EventArgs e)  
  2. {  
  3.     if (dataGridView1.Rows.Count > 0)  
  4.     {  
  5.         SaveFileDialog sfd = new SaveFileDialog();  
  6.         sfd.Filter = "PDF (*.pdf)|*.pdf";  
  7.         sfd.FileName = "Output.pdf";  
  8.         bool fileError = false;  
  9.         if (sfd.ShowDialog() == DialogResult.OK)  
  10.         {  
  11.             if (File.Exists(sfd.FileName))  
  12.             {  
  13.                 try  
  14.                 {  
  15.                     File.Delete(sfd.FileName);  
  16.                 }  
  17.                 catch (IOException ex)  
  18.                 {  
  19.                     fileError = true;  
  20.                     MessageBox.Show("It wasn't possible to write the data to the disk." + ex.Message);  
  21.                 }  
  22.             }  
  23.             if (!fileError)  
  24.             {  
  25.                 try  
  26.                 {  
  27.                     PdfPTable pdfTable = new PdfPTable(dataGridView1.Columns.Count);  
  28.                     pdfTable.DefaultCell.Padding = 3;  
  29.                     pdfTable.WidthPercentage = 100;  
  30.                     pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;  
  31.   
  32.                     foreach (DataGridViewColumn column in dataGridView1.Columns)  
  33.                     {  
  34.                         PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText));  
  35.                         pdfTable.AddCell(cell);  
  36.                     }  
  37.   
  38.                     foreach (DataGridViewRow row in dataGridView1.Rows)  
  39.                     {  
  40.                         foreach (DataGridViewCell cell in row.Cells)  
  41.                         {  
  42.                             pdfTable.AddCell(cell.Value.ToString());  
  43.                         }  
  44.                     }  
  45.   
  46.                     using (FileStream stream = new FileStream(sfd.FileName, FileMode.Create))  
  47.                     {  
  48.                         Document pdfDoc = new Document(PageSize.A4, 10f, 20f, 20f, 10f);  
  49.                         PdfWriter.GetInstance(pdfDoc, stream);  
  50.                         pdfDoc.Open();  
  51.                         pdfDoc.Add(pdfTable);  
  52.                         pdfDoc.Close();  
  53.                         stream.Close();  
  54.                     }  
  55.   
  56.                     MessageBox.Show("Data Exported Successfully !!!""Info");  
  57.                 }  
  58.                 catch (Exception ex)  
  59.                 {  
  60.                     MessageBox.Show("Error :" + ex.Message);  
  61.                 }  
  62.             }  
  63.         }  
  64.     }  
  65.     else  
  66.     {  
  67.         MessageBox.Show("No Record To Export !!!""Info");  
  68.     }  

Now, run the application. When we click on the "Export To Pdf" button, it will ask where to save the file. Put a file name and click on OK. It will generate a pdf file with the given name.

I hope this code will help all readers. Happy Coding.

Next Recommended Reading Exporting Data From DataTable To PDF