You must Sign In to post a response.
  • Category: ASP.NET

    How div export to pdf in asp.net

    Hi,

    i have develop the form. in the form i am showing left side html table
    and right side goodle chart. I have found some code online, but only on html
    table for pdf file. So you do combine this following code. I want direct create file.pdf name
    not with print window and then save.

    code.. for html table
    ====================
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);

    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
    employeelistDiv.RenderControl(htmlTextWriter);

    StringReader stringReader = new StringReader(stringWriter.ToString());
    Document Doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(Doc);
    PdfWriter.GetInstance(Doc, Response.OutputStream);

    Doc.Open();
    htmlparser.Parse(stringReader);
    Doc.Close();
    Response.Write(Doc);
    Response.End();
  • #766217
    you can use iTextSharp to Export employeelistDiv content

    <prev>Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
    employeelistDiv.RenderControl(htmlTextWriter);
    StringReader stringReader = new StringReader(stringWriter.ToString());
    Document Doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(Doc);
    PdfWriter.GetInstance(Doc, Response.OutputStream);
    Doc.Open();
    htmlparser.Parse(stringReader);
    Doc.Close();</prev>

  • #766232
    you can se asp.net panel or DIV tag directly, ASP.Net Panel is nothing but Server Side version of HTML DIV hence in order to use it server side we need to make use of ASP.Net Panel, you need to take use of 'iTextSharp PDF Library' for it
    use 'RenderControl' method of panel to take values from panel and write to HTMLWriter
    see below snippet

    //on export button click
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    pnlPerson.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();

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766241
    Hi,
    Your code looks fine for HTML table/contents but for google chart you need to add some code lines inside it.

    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
    employeelistDiv.RenderControl(htmlTextWriter);
    StringReader stringReader = new StringReader(stringWriter.ToString());
    Document Doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(Doc);
    PdfWriter.GetInstance(Doc, Response.OutputStream);
    Doc.Open();
    htmlparser.Parse(stringReader);
    <font color='green'>/*Newly added code for chart*/</font>
    <font color='brown'>
    using (MemoryStream mStream = new MemoryStream())
    {
    Chart1.SaveImage(mStream, ChartImageFormat.Png);
    iTextSharp.text.Image googleChartImage = iTextSharp.text.Image.GetInstance(mStream.GetBuffer());
    googleChartImage.ScalePercent(70f);
    Doc.Add(googleChartImage);
    }
    </font>
    Doc.Close();
    Response.Write(Doc);
    Response.End();

    You can find whole code and see demo over here:
    http://www.dotnetfox.com/articles/export-chart-control-to-pdf-document-in-Asp-Net-using-itextsharp-and-C-Sharp-1057.aspx


  • Sign In to post your comments