Tuesday, April 2, 2013

How to generate a PDF report from a asp.net Data Table or SharePoint List using Itext C# dll.

I am going to show you that how to generate a PDF from a given data table using Itextsharp.dll and  Itext sharp.pdfa.dll. We can download both of  them from this link. I will create a Webpart which will contain a button on which click we will be able to generate a PDF report for list data on file system.

Here are the steps :

1.) Very first we need to add these two dlls in our GAC.
   
   i)   Itextsharp.dll 
  ii)   Itextsharp.pdfa.dll 

2.)  Now we need to add a webpart. In webpart we need to add only this code :

 protected override void CreateChildControls()
        {
            Button btnExport = new Button();
            btnExport.Text = "Export";
            btnExport.Click += new System.EventHandler(this.btnExport_Click);
            Label objLabel = new Label();
            this.Controls.Add(btnExport);
            this.Controls.Add(objLabel);

        }



        protected void btnExport_Click(object sender, EventArgs e)
        {
           
                    SPWeb objSPWeb = SPContext.Current.Web;
                    SPList objSPList = objSPWeb.Lists["ListName"];
                    DataTable objDT = objSPList.Items.GetDataTable();

                    //Here you can use a Caml Query to do all types of filtering of data and columns.
                    ExportDataToPDFTable(objDT,
"Poc1" , objSPWeb);
                   
        }

        private void ExportDataToPDFTable(DataTable dt, string PDFName, SPWeb objSPWeb)
        {
            Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
            try
            {

                string pdfFilePath = @"D:\POCs\" +
PDFName + ".pdf";

                //Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
                PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Create));

                doc.Open();//Open Document to write

                Font font8 = FontFactory.GetFont("ARIAL", 7);
                Font font9 = FontFactory.GetFont("ARIAL", 8);

                //Write some content
                Paragraph paragraph = new Paragraph("Using ITextsharp I am going to show how to create simple table in PDF document ");

                //DataTable dt = GetDataTable();

                //DataTable dt = dtStructure.Clone();
                //dt.Rows.Add(objDataRow);

              

                if (dt != null)
                {
                    PdfPTable PdfTable = new PdfPTable(dt.Columns.Count);
                    PdfPCell PdfPCell = null;
                    foreach (DataColumn item in dt.Columns)
                    {
                       
                        PdfPCell = new PdfPCell(new Phrase(new Chunk(item.ColumnName, font9)));
                        PdfTable.AddCell(PdfPCell);
                    }
                   
                    //How add the data from datatable to pdf table
                    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

                    doc.Add(paragraph);// add paragraph to the document
                    doc.Add(PdfTable); // add pdf table to the document

                }

            }
            catch (DocumentException docEx)
            {
                //handle pdf document exception if any
            }
            catch (IOException ioEx)
            {
                // handle IO exception
            }
            catch (Exception ex)
            {
                // ahndle other exception if occurs
            }
            finally
            {
                //Close document and writer
                doc.Close();

            }
        }

Through this you will be able to generate PDF with least coding from a DataTable in C#.

No comments:

Post a Comment