Thursday, December 5, 2013

Generate PDF from a html string using ItextSharp in asp.net (c#)

In this post i will show how to create a pdf file from a html string using itextsharp in asp.net.

Below is the code to do this (There may be redundant code in the given sample below. So you can edit the below code as per your needs)

//creating html:

        public void ViewReportPDF(Int32 EmpID)
        {
            SqlCommand cmd = new SqlCommand("USP_GET_EMP_DATA");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@empid", EMPID);
            DataSet ds = GetDataSet(cmd);

            int count = 0;
            string s1 = " <table border='1' width='100%'>";

            string s2 = "";

            for (int j = 0; j <= ds.Tables.Count - 1; j++)
            {
                for (int i = 0; i <= ds.Tables[j].Rows.Count - 1; i++)
                {
                    for (int k = 0; k <= ds.Tables[j].Columns.Count - 1; k++)
                    {
                        if (count == 2)
                        {
                            count = 0;
                        }

                        if (count == 0)
                        {
                            s2 = s2 + "<tr>";
                        }

                        s2 = s2 +
                        "<td>" + ds.Tables[j].Columns[k].ColumnName + "</td>" +
                        "<td>" + ds.Tables[j].Rows[i][k] + "</td>";

                        count++;

                        if (count == 2)
                        {
                            s2 = s2 + "</tr>";
                        }
                    }
                }
            }

            string s3 = "</table>";

            string htmlText1 = s1 + s2 + s3;
            HTMLToPdf(htmlText1);
        }

//creating pdf using itextsharp and saving it in the application folder:

        public void HTMLToPdf(string HTML)
        {
            Document document = new Document(PageSize.A4);
            PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\EMP.pdf", FileMode.Create));
            document.Open();
            iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
            iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
            hw.Parse(new StringReader(HTML));
            document.Close();
            string ss = Request.PhysicalApplicationPath.ToString() + "\\EMP.pdf";
            ShowPdf(ss);
        }

//showing pdf from folder where it was saved.

        private void ShowPdf(string s)
        {
            string fileName = Path.GetFileNameWithoutExtension(s);
            Response.ContentType = "application/pdf";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + "EMP" + ".pdf");
            Response.TransmitFile(Request.PhysicalApplicationPath.ToString() + "\\" + fileName + ".pdf");
            HttpContext.Current.ApplicationInstance.CompleteRequest();

        }

Output:

The output of pdf would be in below structure as given below:


2 comments:

  1. You can generate/convert HMTL to PDF file by using .NET Component for PDF from Aspose. You can find c# code for this here.

    ReplyDelete