Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I am using iTextSharp PDF library to convert plain html into multiple pages PDF file. The problem is with printing footer on every page. I have tried everything, but nothing worked. Please help me to sort out this issue. Below is my complete code:

C#
foreach (DataRow dr in dt.Rows)
            {               
                //htmlTable.Append("<div 
                //style='page-break-before: always'>");
              
                htmlTable.Append("<div style='font-weight:bold;
                    text-align: center;'></div>");               
               
                htmlTable.Append("<table border='1' width='80%'>");
                htmlTable.Append("<tr>");
              
                htmlTable.Append("</tr>");
                int k = 1;
                string qry1 = "select * from tb where No='" + NO + "' 
                       and Name='" + SelectedValue.ToString() + "'";              
                   
                htmlTable.Append("</table>");
                htmlTable.Append("<br/>");
                //
               
                //htmlTable.Append("<div style='text-align: right;
                //'>(" + name + ")</div>");
                htmlTable.Append("<table border='0' float='right'>");
                htmlTable.Append("<tr>");
                htmlTable.Append("<td> </td>");
                htmlTable.Append("<td> </td>");
                htmlTable.Append("<td> </td>");
                htmlTable.Append("<td>(" + name + ")</td>");
                htmlTable.Append("</tr>");
                htmlTable.Append("</table>");
                // htmlTable.Append("<br/>");
                //htmlTable.Append("<div style='width:20%;
                //text-align: right;'>(" + address + ")</div>");
                htmlTable.Append("<table border='0' float='right'>");
                htmlTable.Append("<tr>");
                htmlTable.Append("<td> </td>");
                htmlTable.Append("<td> </td>");
                htmlTable.Append("<td> </td>");
                htmlTable.Append("<td colspan='2'>R/o " + 
                                  address + "</td>");
                htmlTable.Append("</tr>");
                htmlTable.Append("</table>");
                //htmlTable.Append("<div style='position:absolute; 
                //bottom:0; width:100%; height:60px;background:#6cf;'
                //>This is a footer. Page </div>");
                           
                //htmlTable.Append("<br/><br/><
                // br/><br/>");
                //htmlTable.Append("<div style=\"font-family: \'Arial\', sans-serif; color: #ccc; font-size: 10px; margin: 0 auto;text-align:center;bottom:0;position:fixed;\">Simple page footer aligned by center.</div>");                
              
                //htmlTable.Append("<div 
                //style='page-break-after:always;'> </div>");
              
                htmlTable.Append("<newpage />");
            }
            lblResult.Text = htmlTable.ToString();

            //Creating the object of the String Writer.
            StringWriter sw = new StringWriter();

            // Creating the object of HTML Writer and passing the 
            // object of String Writer to HTMl Text Writer
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            //this.Page.RenderControl(hw);
            lblResult.RenderControl(hw);
            // Now whatever is rendered on the page, we will 
            // give it to the object of the String reader so that we can 
            StringReader srdr = new StringReader(sw.ToString());

            // Creating the PDF DOCUMENT using the Document class 
            // from Itextsharp.pdf namespace
            Document pdfDoc = new Document(PageSize.A4, 50F, 50F, 50F, 0.2F);

            // HTML Worker allows us to parse the HTML Content to the 
            // PDF Document. To do this, we will pass the object 
            // of Document class as a Parameter.

            //  HTMLWorker hparse = new HTMLWorker(pdfDoc);

            HTMLWorkerExtended hparse = new HTMLWorkerExtended(pdfDoc);
            //using (var htmlWorker = new HTMLWorkerExtended()
            //{
            //    htmlWorker.Open();
            //    htmlWorker.Parse(htmlViewReader);
            //}

            // Finally we write data to PDF and open the Document
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();

            // Now we will pass the entire content that is stored 
            // in String reader to HTML Worker object to achieve 
            // the data from to String to HTML and then to PDF.
            hparse.Parse(srdr);

            pdfDoc.Close();

            //Now finally we write to the PDF Document 
            //using the Response.Write method.
            Response.ContentType = "application/pdf"; // Setting the 
                                                      // application
                                                      // Assigning the 
                                                      // header
            Response.AddHeader("content-disposition", 
                           "attachment;filename=filename.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Write(pdfDoc);
            Response.End();
        }

public class HTMLWorkerExtended : HTMLWorker
{ public HTMLWorkerExtended(Document document) : base(document) {

}
public override void StartElement(string tag, Hashtable h)
{
    if (tag.Equals("newpage"))
        document.Add(Chunk.NEXTPAGE);
    else
        base.StartElement(tag, h);
}
}


What I have tried:

HTML
htmlTable.Append("<div style='position:absolute; bottom:0; width:100%; height:60px;background:#6cf;'>This is a footer. Page </div>");
Posted
Updated 13-Sep-23 4:38am
v2
Comments
Richard Deeming 30-Aug-23 3:33am    
Beyond that, HTML doesn't really have a concept of "pages" and "footers". (There's the CSS paged media[^] module, but it doesn't have the option to designate an element as a header or footer.)

You'll need to consult the iTextSharp documentation to see if it provides any way to specify headers and footers in its HTML-to-PDF conversion routines.

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