Monday, 4 November 2013

How to export ASP.Net Web Page to PDF



In this article I will explain how to export ASP.Net web page to PDF.

First, you will need to download ITextsharp and add its reference to your project. ITextsharp is a free HTML to PDF Library. You can download it using the following link.

http://sourceforge.net/projects/itextsharp/

The HTML markup of the page contains a sample page with an image, some text and an HTML table. Also there’s an Export PDF button on click of which we will export the page to PDF.



Below is the aspx page design.

<form id="form1" runat="server">
    <table>
        <tr>
            <td>
                <asp:Panel ID="pnImage" runat="server">
                    <table>
                        <tr>
                            <td>
                                <asp:GridView ID="gvProducts" runat="server" AllowPaging="True" Width="800px" AutoGenerateColumns="false"
                                    PageSize="20">
                                    <Columns>
                                        <asp:TemplateField HeaderText="Catalog No">
                                            <ItemTemplate>
                                                <a href='ProductSpecification.aspx?ItemCode=<%#Eval("Catalog No") %>' style="font-size: small;
                                                    color: Black;" class="underLine" target="_DBWindow">
                                                    <%#Eval("Catalog No")%></a>
                                            </ItemTemplate>
                                           
</asp:TemplateField>
                                       
                                    </Columns>
                                </asp:GridView>
                            </td>
                        </tr>
                    </table>
                    <asp:Image ID="imgHeader" runat="server" ImageUrl="~/images/index_02.jpg" />
                </asp:Panel>
                <asp:Button ID="btnExportPDF" runat="server" Text="Export" OnClick="btnExportPDF_Click" />
            </td>
        </tr>
    </table>
    </form>

Use the below Name Spaces in aspx.cs page.

using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

Write the below code in Export PDF button click event.

  protected void btnExportPDF_Click(object sender, EventArgs e)
    {
        dsGetProducts = ObjBusiness.GetAllProducts();
        gvProducts.DataSource = dsGetProducts;
        gvProducts.DataBind();

        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=First.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();
    }

If you get below error follow this URL.

URL :

http://www.aspsnippets.com/Articles/RegisterForEventValidation-can-only-be-called-during-Render.aspx

Error:

 RegisterForEventValidation can only be called during Render();

No comments:

Post a Comment