Click here to Skip to main content
15,905,073 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,


I would like to Export Datable to PDF using windows application

pls let me know the way

Thanks & Regards,
Soumya
Posted
Updated 6-Feb-18 0:55am
v3

see below link you may get some idea
How to export datatable to PDF using C# windows forms[^]
 
Share this answer
 
Hi,
I think you should use a dll i.e. iTextSharp.dll for it. Just download it. and use it.

Regards
 
Share this answer
 
Thank u prasad for your response

I could solve with below coding
C#
public void ExportToPdf(DataTable dt)
    {
      
        Document document = new Document();
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("c://newchk.pdf", FileMode.Create));
        document.Open();
        //iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("c://ggi logo.bmp");
        //document.Add(img);
        iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 5);
        //float[] columnDefinitionSize = { 22F, 22F, 12F, 7.75F, 7.77F, 7.77F, 7.77F, 7.77F, 10.88F, 10.88F, 10.88F, 4.75F, 7.77F, 7.77F, 7.77F, 7.77F, 7.77F, 7.77F, 9F };

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


        ////table.AddCell(cell);
        cell.Colspan = dt.Columns.Count;

            //cell.Border = 0;

            //cell.HorizontalAlignment = 1;
        foreach (DataColumn c in dt.Columns)
        {

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

        //cell.BackgroundColor = new iTextSharp.text.Color(0xC0, 0xC0, 0xC0);


        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();
    }
 
Share this answer
 
v2

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