Click here to Skip to main content
16,017,922 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Export datagridview data to pdf
Posted
Comments
ManishSharma_dotnet 5-Mar-14 7:24am    
Download the dll of itextSharp.dll

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");

t1.AddCell(cid);
t1.AddCell(cname);

foreach (DataGridViewRow rows in dataGridView1.Rows)
{

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!");
}

1 solution

Do you ever tried something? A simple search in CodeProject would let you these results[^] and also in Google[^]
Also see,
How to convert datagridview to pdf in c#[^]
Exporting_GridView_to_PDF_Document.aspx[^]
How-to-writeexport-datatable-to-pdf-using-c-windows-forms[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900