Click here to Skip to main content
15,906,625 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have successfully exported a HTML table in c#.net to pdf file.

But my problem is how to repeat header on each page of pdf file.

Below is my code.

C#
List= GetCurrentiList();

            HtmlTable tbl = new HtmlTable();
            HtmlTableRow tr = new HtmlTableRow();
            HtmlTableCell td = new HtmlTableCell();
            td.InnerText = "Product ID";
            tr.Cells.Add(td);
            td = new HtmlTableCell();
            td.InnerText = " Code";
            td.Width = "150px";
            tr.Cells.Add(td);
            td = new HtmlTableCell();
            td.InnerText = "Description";
            td.Width = "180px";
            tr.Cells.Add(td);
            td = new HtmlTableCell();
            td.InnerText = "roduct Quantity";
            tr.Cells.Add(td);
            td = new HtmlTableCell();
            td.InnerText = "Price";
            tr.Cells.Add(td);
            tr.BgColor = "#00ffff";
            tbl.Rows.Add(tr);

  decimal totalprice = 0;
            foreach (ItemRec p in List)
            {
                if (p.Id != 0)
                {
                    HtmlTableRow tr1 = new HtmlTableRow();
                    HtmlTableCell td1 = new HtmlTableCell();
                    td1.InnerText = p.Id.ToString();
                    tr1.Cells.Add(td1);
                    td1 = new HtmlTableCell();
                    td1.InnerText = p.Code;
                    tr1.Cells.Add(td1);
                    td1 = new HtmlTableCell();
                    td1.InnerText = p.Name;
                    tr1.Cells.Add(td1);
                    td1 = new HtmlTableCell();
                    td1.InnerText = p.Quantity.ToString();
                    tr1.Cells.Add(td1);
                    td1 = new HtmlTableCell();
                    td1.InnerText = p.UnitPrice.ToString();
                    tr1.Cells.Add(td1);
                    tbl.Rows.Add(tr1);
                    totalprice += p.UnitPrice;
                }
            }
            HtmlTableRow tr2 = new HtmlTableRow();
            HtmlTableCell td2 = new HtmlTableCell();
            td2.InnerText = "Total Price = ";
            td2.ColSpan = 4;
            td2.Align = "right";
            tr2.Cells.Add(td2);

            td2 = new HtmlTableCell();
            td2.InnerText = totalprice.ToString();
            tr2.Cells.Add(td2);
            tbl.Rows.Add(tr2);
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition",
            "attachment;filename=DataTable.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            tbl.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();


Thanks in advance.
Posted
Updated 27-Sep-12 21:13pm
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