Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Guys,I want to know
How to export datagridview data in pdf i was googling but i can't found yet so please guys if you know about it please give me the solution with your code please don't put any link of any articles.

What I have tried:

public void ExportToPdf(DataTable dt)
               {
                   Document document = new Document();
                   PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("c://sample.pdf", FileMode.Create));
                   document.Open();
                   iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 5);

                   PdfPTable table = new PdfPTable(dt.Columns.Count);
                   PdfPRow row = null;
                   float[] widths = new float[] { 4f, 4f, 4f, 4f };

                   table.SetWidths(widths);

                   table.WidthPercentage = 100;
                   int iCol = 0;
                   string colname = "";
                   PdfPCell cell = new PdfPCell(new Phrase("Products"));

                   cell.Colspan = dt.Columns.Count;

                   foreach (DataColumn c in dt.Columns)
                   {

                       table.AddCell(new Phrase(c.ColumnName, font5));
                   }

                   foreach (DataRow r in dt.Rows)
                   {
                       if (dt.Rows.Count > 0)
                       {
                           table.AddCell(new Phrase(r[0].ToString(), font5));
                           table.AddCell(new Phrase(r[1].ToString(), font5));
                           table.AddCell(new Phrase(r[2].ToString(), font5));
                           table.AddCell(new Phrase(r[3].ToString(), font5));
                       }
                   } document.Add(table);
                   document.Close();
               }
Posted
Updated 19-Sep-16 1:45am
Comments
[no name] 18-Sep-16 16:51pm    
"give me the solution with your code", gimme code is not a question and we are not a write code to order service.
Member 9983063 18-Sep-16 17:31pm    
Sorry sir that was not my mean it's mean if you know about it so please tell me how can i do this bcz i have visits all links but it's not working for me
[no name] 18-Sep-16 17:41pm    
One has to wonder how many years you need to be a developer before you realize "it not work" means absolutely nothing. http://www.bing.com/search?q=itextsharp+export+datatable+to+pdf&src=IE-SearchBox&FORM=IENTSR&pc=EUPP_

You haven't stated an issue with your code...only that you want someone to do the work for you. This isn't an issue that has gone unsolved, many people have done this at one time or another. Simply use google.

This returns 11,700 results

Export datatable to pdf itextsharp[^]

c# - Convert Datatable to PDF - Stack Overflow[^]

c# - How can we export datatable to PDF using iTextSharp? - Stack Overflow[^]

http://www.codeproject.com/Questions/647786/How-can-we-export-datatable-to-PDF-using-iTextShar[^]

How can we export datatable to PDF using iTextSharp | ASP.NET | C# | MVC | EF | Cloud | SQL Server | IIS | Tutorials[^]

Now if you'd like to provide a clear explanation of what it is you are trying to do and why the code you've provided isn't working that may be the first step. Till then, use google and the links i've provided to accomplish what it is you want.
 
Share this answer
 
Refer this link Convert Gridview to PDF
 
Share this answer
 

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