Labels

slider

Recent

Navigation

How can we export datatable to PDF using iTextSharp

Export datatable to PDF, export gridview to PDF, export datatable to pdf using itextsharp in asp.net

Introduction

I have illustrated below with example how can we export data table to PDF using iTextSharp

Step 1. We can download itextsharp.dll from the website http://sourceforge.net/projects/itextsharp/
Step 2. Please click on add exiting item to add iTextSharp.dll in the "Bin" folder.
 iTextSharp.dll


Step 3. In the next step, put downloaded iTextSharp.dll in the project folder structure to add reference of the iTextSharp.dll in the application.

iTextSharp.dll  
 

Step 4. You may check now that dll reference have been added in your application. 

iTextSharp.text

Step 5. Now I have provided below c# code snippet as given below:

 protected void btnPDF_Click(object sender, ImageClickEventArgs e)
    {
        DataTable dtn = new DataTable();
        dtn = GetDataTable();
        dtPDF = dtn.Copy();
        for (int i = 0; i <= dtn.Rows.Count - 1; i++)
        {
            ExportToPdf(dtPDF);
        }
     }

 public void ExportToPdf(DataTable myDataTable)
    {
        Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);
        try
        {
            PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
            pdfDoc.Open();
            Chunk c = new Chunk("" + System.Web.HttpContext.Current.Session["CompanyName"] + "", FontFactory.GetFont("Verdana", 11));
            Paragraph p = new Paragraph();
            p.Alignment = Element.ALIGN_CENTER;
            p.Add(c);
            pdfDoc.Add(p);
            string clientLogo = Server.MapPath(".") + "/logo/tpglogo.jpg";
            string imageFilePath = Server.MapPath(".") + "/logo/tpglogo.jpg";
            iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
            //Resize image depend upon your need   
            jpg.ScaleToFit(80f, 60f);
            //Give space before image   
            jpg.SpacingBefore = 0f;
            //Give some space after the image   
            jpg.SpacingAfter = 1f;
            jpg.Alignment = Element.HEADER;
            pdfDoc.Add(jpg);
            Font font8 = FontFactory.GetFont("ARIAL", 7);
            DataTable dt = myDataTable;
            if (dt != null)
            {
                //Craete instance of the pdf table and set the number of column in that table  
                PdfPTable PdfTable = new PdfPTable(dt.Columns.Count);
                PdfPCell PdfPCell = null;
                for (int rows = 0; rows < dt.Rows.Count; rows++)
                {
                    for (int column = 0; column < dt.Columns.Count; column++)
                    {
                        PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), font8)));
                        PdfTable.AddCell(PdfPCell);
                    }
                }
                //PdfTable.SpacingBefore = 15f; // Give some space after the text or it may overlap the table            
                pdfDoc.Add(PdfTable); // add pdf table to the document   
            }
            pdfDoc.Close();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment; filename= SampleExport.pdf");
            System.Web.HttpContext.Current.Response.Write(pdfDoc);
            Response.Flush();
            Response.End();
            //HttpContext.Current.ApplicationInstance.CompleteRequest();  
        }
        catch (DocumentException de)
        {
            System.Web.HttpContext.Current.Response.Write(de.Message);
        }
        catch (IOException ioEx)
        {
            System.Web.HttpContext.Current.Response.Write(ioEx.Message);
        }
        catch (Exception ex)
        {
            System.Web.HttpContext.Current.Response.Write(ex.Message);
        }
    }   

Suggested Reading

Share

Anjan kant

Outstanding journey in Microsoft Technologies (ASP.Net, C#, SQL Programming, WPF, Silverlight, WCF etc.), client side technologies AngularJS, KnockoutJS, Javascript, Ajax Calls, Json and Hybrid apps etc. I love to devote free time in writing, blogging, social networking and adventurous life

Post A Comment:

3 comments:

  1. Hai Anjan thank you so much,,,worked properly for me.

    ReplyDelete
  2. There are numerous product apparatuses for making and controlling PDF records. A few apparatuses are free devices and others are most certainly not. https://de.altoconvertpdftojpg.com/

    ReplyDelete