How to Export ASP.NET Form to PDF

No.of Views3743
Bookmarked0 times
Downloads 
Votes0
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 info@codegain.com

 

Introduction

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 asp.net page with complex controls, you need simply to install iTextSharp PDF tool, easy and free tool to export your page to pdf.

Implementation

Here is the steps:

1) Install iTextSharp tool : http://sourceforge.net/projects/itextsharp/
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.ClearContent();       

                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. 

                myDiv.RenderControl(htextw);      

                Document document = new Document();       

                PdfWriter.GetInstance(document, Response.OutputStream);   

                document.Open();         

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

                HTMLWorker htmlworker = new HTMLWorker(document);  

               htmlworker.Parse(str);       

               document.Close();        

               Response.Write(document);  

               Response.End();   

         }

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 asp.net forms:

public override void VerifyRenderingInServerForm(Control control)

 { /* Do nothing */ }

Hope this helps.

 
Sign Up to vote for this article
 
About Author
 
Mostafa Elzoghbi
Occupation-
Company-
Member Type-Junior
Location-United States
Joined date-07 Jun 2010
Home Page-http://moustafa-arafa.blogspot.com
Blog Page-http://moustafa-arafa.blogspot.com
MCSD,MCT,MCTS,C# MVP
 
 
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 asp.net.
    Published Date : 05/Jan/2011
    In this snippet I will explain how to add controls dynamically in asp.net 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 asp.net giving problems and also it not working properly with events
    Published Date : 03/Jan/2011
Comments
By:raviDate Of Posted:1/24/2013 4:39:04 AM
Error
Got error :- Input string was not in a correct format.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
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