Its just easy to create and also I preferred to the way of implement this functionality.
just create sample application and copy, paste. It will run perfectly.
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Export WebPage As PDF With Image Sample</title> </head> <body> <form id="form1" runat="server"> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <div> <asp:Image ID="Image1" runat="server" ImageUrl="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsbW0F_Hf5Sms8GITXm6NX-Wk_dCtDPrDzKuj9Ej2wfDOHGeabYv_PBBG2IdFhzz6UMoLh2Ew8o_PzqUpukifu-uAQsARxY-BIlbSIAWgWDvix598hwyuXGVf-9jVS0pEAeIaeXQM4bxDa/s1600/etechpulse_logo.png" /> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> <br /> <asp:Button ID="btnExportasPDF" runat="server" Text="Export WebPage As PDF With Image" OnClick="btnExportasPDF_Click" /> </div> <div> <asp:Panel ID="Panel1" runat="server"> <asp:GridView ID="GridView2" runat="server"> </asp:GridView> </asp:Panel> <br /> <asp:Button ID="btnExportPanelPDF" runat="server" Text="Export Panel As PDF" OnClick="btnExportPanelPDF_Click"/> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; // include iTextSharp using iTextSharp.text; using iTextSharp.text.html.simpleparser; using iTextSharp.text.pdf; using System.Text; using System.IO; using System.Data; namespace Testing { public partial class iTextSharpWebPageExportPDF : System.Web.UI.Page { DataTable dt = new DataTable(); protected void Page_Load(object sender, EventArgs e) { dt.Columns.Add("EmpId", typeof(int)); dt.Columns.Add("Name", typeof(string)); dt.Columns.Add("Address", typeof(string)); dt.Columns.Add("Date", typeof(DateTime)); // // Here we add five DataRows. // dt.Rows.Add(25, "Rk", "Gurgaon", DateTime.Now); dt.Rows.Add(50, "Sachin", "Noida", DateTime.Now); dt.Rows.Add(10, "Nitin", "Noida", DateTime.Now); dt.Rows.Add(21, "Aditya", "Meerut", DateTime.Now); dt.Rows.Add(100, "Mohan", "Banglore", DateTime.Now); GridView1.DataSource = dt; GridView1.DataBind(); GridView2.DataSource = dt; GridView2.DataBind(); Label1.Text = "Noida"; } protected void btnExportasPDF_Click(object sender, EventArgs e) { Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); this.Page.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); } protected void btnExportPanelPDF_Click(object sender, EventArgs e) { Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); Panel1.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); } public override void VerifyRenderingInServerForm(Control control) { // } } }
No comments:
Post a Comment