Download the dll of itextSharp.dll
<http://sourceforge.net/projects/itextsharp/>
and try this code
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
private void btnExport_Click(object sender, EventArgs e)
{
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("d:\\Test.pdf", FileMode.Create));
doc.Open();//Open Document to write
Paragraph paragraph = new Paragraph("data Exported From DataGridview!");
//Create table by setting table value
Table t1 = new Table(2);
DataTable dt = (DataTable)dataGridView1.DataSource;
//Create Table Header
Cell cid = new Cell("ID");
Cell cname = new Cell("Name");
cid.BackgroundColor = iTextSharp.text.Color.GRAY;
cname.BackgroundColor = iTextSharp.text.Color.GRAY;
t1.AddCell(cid);
t1.AddCell(cname);
foreach (DataGridViewRow rows in dataGridView1.Rows)
{
//check checkbox of datagridview is checked
if (Convert.ToBoolean(dataGridView1.Rows[rows.Index].Cells[0].Value))
{
string id = dataGridView1.Rows[rows.Index].Cells["empid"].Value.ToString();
string name = dataGridView1.Rows[rows.Index].Cells["ename"].Value.ToString();
//Create Cells
Cell c2 = new Cell(id);
Cell c1 = new Cell(name);
//Adding cells
t1.AddCell(c1);
t1.AddCell(c2);
}
}
doc.Add(paragraph);
doc.Add(t1);
doc.Close(); //Close document
//
MessageBox.Show("PDF Created!");
}