How to Export ASP.NET Form to PDF

No.of Views4050
Bookmarked0 times
By  Mostafa Elzoghbi   On  13 Jun 2011 04:06:57
Tag : ASP.NET , Miscellaneous
I have seen most of forums, professionals asking how to export ASP.NET to PDF.Because PDF format likes all users almost.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at



I was trying to export the content of an ASP.NET page in my project to PDF, and after i spent few hours to figure out how to achieve this using standard Resposnse.Output() with pdf format, I ended up having different errors & bugs without successfully export a simple html page to pdf.
Having complex page with complex controls, you need simply to install iTextSharp PDF tool, easy and free tool to export your page to pdf.


Here is the steps:

1) Install iTextSharp tool :
2) Add itextsharp.dll in your project.
3) Create your function to export to PDF as follows:

C# Code

protected void ExportToPDF()  


                // Start exporting page content

                string attachment = "attachment; filename=MostafaElzoghbi.pdf"; 


                Response.AddHeader("content-disposition", attachment); 

                Response.ContentType = "application/pdf";    

                StringWriter stw = new StringWriter();   

                HtmlTextWriter htextw = new HtmlTextWriter(stw); 

                // We want to render the whole page, you can change this with the control name or portion you want to export. 


                Document document = new Document();       

                PdfWriter.GetInstance(document, Response.OutputStream);   


                StringReader str = new StringReader(stw.ToString()); 

                HTMLWorker htmlworker = new HTMLWorker(document);  






If you get this error: A control has to be placed inside a form tag with runat=server attribute.

Just add this function in your code to override the pre render validation step in forms:

public override void VerifyRenderingInServerForm(Control control)

 { /* Do nothing */ }

Hope this helps.

Sign Up to vote for this article
About Author
Mostafa Elzoghbi
Member Type-Junior
Location-United States
Joined date-07 Jun 2010
Home Page-
Blog Page-
Other popularSectionarticles
    Auto Growing TextBox or TextArea in ASP.NET
    Published Date : 08/May/2010
    In this code snippet, you will learn how to bind DropdownList within the ListView in ASP.NET. The ListView is powerful control and fully customizable using templates.
    Published Date : 10/Oct/2010
    In this codesnippet, i will show How to Delete Row in GridView using JQuery in ASP.NET.
    Published Date : 20/Jul/2011
    In this snippet, I will show how to format a cell and apply style in gridview using JQuery. Sometimes we may need to apply the format for a particular cell based on the cell value; it can be done in within DataRowBound event in
    Published Date : 05/Jan/2011
    In this snippet I will explain how to add controls dynamically in and register events for the controls and make it work events perfectly. Last week I have read the forums many readers asking about add controls dynamically in giving problems and also it not working properly with events
    Published Date : 03/Jan/2011
By:raviDate Of Posted:1/24/2013 4:39:04 AM
Got error :- Input string was not in a correct format.
Leave a Reply
Display Name:
(not display in page for the security purphase)
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top