Author:
Filed Under: ASP.Net
Published Date: Nov 25, 2011
Views: 22214
 

Abstract: Here Mudassar Ahmed Khan has explained how to export ASP.Net Web Page with images to PDF using ITextsharp PDF conversion library


Download Sample 15
In this article I will explain how to export ASP.Net Web Page to PDF i.e. Portable Document Format.
So let’s start building the sample.
 
Download ITextsharp
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 download link
 
HTML Markup
The HTML markup of the page contains a sample page with an image, some text and an HTML table. Also there’s an export button on click of which we will export the page to PDF.
<form id="form1" runat="server">
    <div>
    <img src = "http://www.aspsnippets.com/images/Blue/Logo.png" /><br />
    </div>
    <div style = "font-family:Arial">This is a test page</div>
    <div>
    <table border = "1" width = "100">
    <tr><td>Name</td><td>Age</td></tr>
    <tr><td>John</td><td>11</td></tr>
    <tr><td>Sam</td><td>13</td></tr>
    <tr><td>Tony</td><td>12</td></tr>
    </table>
    </div>
    <div>
    <asp:Button ID="btnExport" runat="server" Text="Export" onclick="btnExport_Click" /></div>
</form>
 
Namespaces
You will need to import the following namespaces in your code.
C#
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
 
VB.Net
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf
 
Exporting the page to PDF
The below event handler is executed on the click of the export button.
C#
protected void btnExport_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();
}
 
VB.Net
Protected Sub btnExport_Click(sender As Object, e As EventArgs)
    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf")
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Dim sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)
    Me.Page.RenderControl(hw)
    Dim sr As New StringReader(sw.ToString())
    Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 0.0F)
    Dim htmlparser As New HTMLWorker(pdfDoc)
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
    pdfDoc.Open()
    htmlparser.Parse(sr)
    pdfDoc.Close()
    Response.Write(pdfDoc)
    Response.[End]()
End Sub
 
Note: If you get the following error. Refer this article RegisterForEventValidation can only be called during Render()
Server Error in 'ASP.Net' Application.

RegisterForEventValidation can only be called during Render();
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: RegisterForEventValidation can only be called during Render();
 
Screenshots
ASP.Net Page
Export ASP.Net Web Page to PDF using Itextsharp
Exported PDF
Export ASP.Net Web Page to PDF using Itextsharp
Downloads
You can download the complete source code in VB.Net and C# using the download link provided below.
ExportPagetoPDFinASP.Net.zip
 





Related Articles

Comments


Add Comment

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
Please do not post code, scripts or snippets.

Name*: Required
Email*: Required
Comment*: Required
Security code*: CaptchaInvalid Security Code