Sei sulla pagina 1di 12

25 Nov 2011 Mudassar Khan 21 Comments 239984 Views

ASP.Net

‫ أﻟف‬13 ‫أﻋﺟﺑﻧﻲ‬ ‫ﻣﺗﺎﺑﻌﺔ‬ 1,490 Follow @ASPSnippets

Here Mudassar Ahmed Khan has explained how to export ASP.Net Web Page with images to PDF
using ITextsharp PDF conversion library
Download Download Free Files API
In this article I will explain how to export ASP.Net Web Page to PDF i.e. Portable Document For
mat.

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

Download ITextsharp

HTML Markup

The HTML markup of the page contains a sample page with an image, some text and an HTML t
able. 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 = "//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 call
ed 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 we
b request. Please review the stack trace for more information about the error and w
here it originated in the code.

Exception Details: System.InvalidOperationException: RegisterForEventValidation can


only be called during Render();

Screenshots

ASP.Net Page
Exported PDF

Downloads
You can download the complete source code in VB.Net and C# using the download link provided
below.

Download

Download Free Files API


Export ASP.Net Panel contents to PDF using iTextSharp
Here Mudassar Ahmed Khan has explained how to export ASP.Net Panel control to PDF (Portable
Document Format) Document using iTextSharp Free PDF Library.
Export ASP.Net Chart Control to PDF Document using iTextSharp Library
Here Mudassar Ahmed Khan has explained how to export ASP.Net Chart Control to PDF
document using iTextSharp PDF Conversion Library.
Export Repeater Control to PDF Document in ASP.Net using iTextSharp
Here Mudassar Ahmed Khan has explained with and example and attached sample code, how to
export Repeater Control to PDF Document in ASP.Net using iTextSharp PDF Conversion Library
and C# and VB.Net.
Export HTML DIV contents to PDF using iTextSharp in ASP.Net
Here Mudassar Ahmed Khan has explained how to export ASP.Net Panel control (which is
rendered as HTML DIV) to PDF (Portable Document Format) Document using iTextSharp Free
PDF Library. 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.
How to generate and download PDF Report from database in ASP.Net using iTextSharp C#
and VB.Net
Here Mudassar Ahmed Khan has explained with an example and attached sample code, create
and download of PDF Report from SQ L Server database using the free iTextSharp Generation
Library in ASP.Net using C# and VB.Net. The Report generated is complete one and contains a
header with the Logo, Name and Address of the company. The Employee details contains Picture
of the Employee, Name and other employee details.
Export Paging enabled Gridview to PDF with Formatting using iTextSharp in ASP.Net
Here Mudassar Ahmed Khan has explained with an example and attached sample code, how to
export all pages of a GridView with paging enabled to Adobe PDF File with Formatting, Styles and
Colors using iTextSharp and C# and VB.Net
Export GridView to Password Protected ( Secured ) PDF using iTextSharp in ASP.Net
Here Mudassar Ahmed Khan has explained with an example and attached sample code, how to
export ASP.Net GridView to Password Protected (Secured) PDF using iTextSharp Library in
ASP.Net with C# and VB.Net.
Create Password Protected ( Secured ) PDF using iTextSharp in ASP.Net
Here Mudassar Ahmed Khan has explained with an example and attached sample code, how to
create Password Protected (Secured) PDF using iTextSharp Library in ASP.Net with C# and
VB.Net.
Export ASP.Net GridView Arabic, Persian or Urdu Language Characters to PDF using
iTextSharp
Here Mudassar Ahmed Khan has explained with an example and attached sample code, how to
export ASP.Net GridView Arabic, Persian or U rdu Language Characters to PDF using iTextSharp
PDF Conversion Library.
iTextSharp: Export ASP.Net GridView with Devanagari, Hindi or Marathi Characters to PDF
Here Mudassar Ahmed Khan has explained with an example and attached sample code, how to
export GridView with Devanagari, Hindi or Marathi Language Characters to PDF using iTextSharp
PDF Conversion library
Export Windows Forms DataGridView to PDF using iTextSharp, C# and VB.Net
Here Mudassar Ahmed Khan has explained how to export DataGridView data to PDF file in
Windows Forms (WinForms) Applications using iTextSharp PDF conversion library, C# and
VB.Net. DataGridView cannot be exported directly to PDF file and hence need to make use of
iTextSharp Table for this purpose.
iTextSharp: Generate PDF in Memory and send as Email Attachment using C#, VB.Net and
ASP.Net
Here Mudassar Ahmed Khan has explained how to dynamically generate PDF in Memory from
HTML using iTextSharp and send the generated PDF as Email Attachment using C# and VB.Net.
U sing iTextSharp Library, the PDF is generated from HTML string in memory by making use of
MemoryStream class and then the same is attached to the MailMessage object and send as email
attachment.
ASP.Net iTextSharp System.Security.SecurityException: That assembly does not allow
partially trusted callers
Here Mudassar Ahmed Khan has explained how to resolve the error
System.Security.SecurityException: That assembly does not allow partially trusted callers when
using iTextSharp Library DLL on a server that does not allow Full Trust to the Website. Generally
we face this issue when using Shared Hosting Servers and hence to resolve this issue we need to
modify the iTextSharp DLL to allow partially trust callers.
iTextSharp: Add Page numbers to existing PDF using C# and VB.Net
Here Mudassar Ahmed Khan has explained how to add page numbers to existing PDF file using
iTextSharp in C# and VB.Net. The pages of the PDF file will be read and then using the
PdfStamper class page numbers will be written to each page of the PDF document.
Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type
'iTextSharp.text.Paragraph'
Here Mudassar Ahmed Khan has explained solution to the following error (exception) when we use
HtmlWorker to convert HTML string to PDF using iTextSharp PDF conversion library. U nable to
cast object of type ' iTextSharp.text.html.simpleparser.CellWrapper' to type
' iTextSharp.text.Paragraph' . One of my readers reported me the above error when exporting
ASP.Net GridView to PDF when he used the latest version of iTextSharp DLL instead of the one
provided in the article sample.

MixfedeFeb 07 , 2012 01: 02 PM 190.194.43.53

Gooooodddd.. excelent short and simple... thanks

HiteshApr 11, 2012 09: 04 AM 10.10.136 .6

mind Blowing Example


thats wat i want...

mohammad ismail akheelMay 20, 2012 09: 05 AM 94.96 .99.115

Jaz akAllahu Khairan wa ahsanuljaz a.


good short and simple.

Emmanuel ThompsonMay 21, 2012 03: 05 PM 197 .255.118.156

Ho brother I just love it your articles are like light in the dearness.Simple and
straightforward. I wanted to q uit programming in asp.do net but with your help I beginning
to enjoy it.
God richly bless you

Ron ThomasMay 28, 2012 02: 05 PM 122.16 6 .158.51


Terrific and effective work dude. god bless you

More

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.

‫ أﻟف‬13 ‫أﻋﺟﺑﻧﻲ‬ ‫ﻣﺗﺎﺑﻌﺔ‬ 1,490 Follow @ASPSnippets

Name
Name
Email
Email
Comment
Comment

Security code:

Add Comment

What our readers say

Furqan
Amaz ing website, Great articles, To the point content.
Keep up the good work.
Ram Chedda
This is a beautiful site and a place to worship for beginners... : )
Vishant
Your code is very helpful and usable..
Thank you for giving help for code and wish to always help us like this.
Vishant
EMMANA
Hi Khan you are a gift to your generation. Much love from me and your fans from Nigeria
Juzer Hussain
Thanks for all these articles Mudassar.They come handy just when one needs them.

Subscribe

Please enter your email address:

Subscribe
All rights reserved | Privacy Policy | Powered by Excelasoft

Potrebbero piacerti anche