代码之家  ›  专栏  ›  技术社区  ›  Pankaj

将页脚HTML打印为PDF iTextSharp PdfWriter

  •  0
  • Pankaj  · 技术社区  · 2 年前

    我正在使用iTextSharp PDF库将纯html转换为多页PDF文件。问题是在每一页上都打印页脚。我什么都试过了,但都没用。请帮我解决这个问题。以下是我的完整代码:-

    htmlTable.Append("<div style='position:absolute; bottom:0; width:100%; height:60px;background:#6cf;'>This is a footer. Page </div>");
    
                foreach (DataRow dr in dt.Rows)
                {
                   
                    //htmlTable.Append("<div style='page-break-before: always'>");
                  
                    htmlTable.Append("<div style='font-weight:bold;text-align: center;'><u></u></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>&nbsp;</td>");
                    htmlTable.Append("<td>&nbsp;</td>");
                    htmlTable.Append("<td>&nbsp;</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>&nbsp;</td>");
                    htmlTable.Append("<td>&nbsp;</td>");
                    htmlTable.Append("<td>&nbsp;</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;'>&nbsp;</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 we what ever 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(文档文档):base(文档) {

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

    }

    0 回复  |  直到 2 年前