Click here to Skip to main content
15,898,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HOW TO CONVERT ASPX PAGE INTO PDF FILE AT RUNTIME?
I HAVE TRIED DIFFERENT CODES BUT NONE OF THEM IS WORKING OUT FOR ME?PLZ HELP ME OUT,TRIED WITH ITEXTSHARP,A PDF FILE IS GETTING CREATED BUT ONLY WITH STATIC CONTROLS LIKE LABELS,PANELS ETC CREATED DURING HTML DESIGN PART.AM NOT ABLE TO ENTER TEXT DYNAMICALLY IN TEXTBOX AND DISPLAY IN PDF FILE.
PLZ HELP ME OUT WITH THIS.THANKS IN ADVANCE.
Posted
Updated 21-Mar-17 23:59pm
Comments
Bala Selvanayagam 10-Oct-11 5:02am    
Can you please not use UPPER CASE when posting questions. Upper case questions are taken as rude behaviour and does not motivate any one to answer/help you.

Please use the "improve question" link and amend your question to proper casing.

1 solution

you need to use something like MigraDoc from PDFSharp to render HTML to PDF. LINK: http://www.pdfsharp.com/[^]

And now to create the HTML output including server side controls, what u need to do is, put the whole pages codes inside a single user control and follow the link to generate the HTML on runtime which will produce the output html which you need to then feed to the MigraDoc to create the PDF document.

C#
StringBuilder SB = new StringBuilder();
StringWriter SW = new StringWriter(SB);
HtmlTextWriter htmlTW = new HtmlTextWriter(SW);
UCDummy.Visible = true;
UCDummy.RenderControl(htmlTW);
WhereEverIWantItToGo = SB.ToString();
UCDummy.Visible = false;


http://www.velocityreviews.com/forums/t104419-render-and-get-html-from-usercontrol.html[^]

I have used this technique before, but can't find the code snippet now. Send me an email if u need it, i will have to do some digging.

--------------------------- NEW CODE SAMPLE ---------------------

let say u want to render the whole aspx page and get the output in html string which u can then push it to pdf generator:

System.Text.StringBuilder sb = new StringBuilder();
System.IO.StringWriter tw = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter ht = new HtmlTextWriter(tw);
this.RenderControl(ht);				
string body = sb.ToString();


just as a tip.. before doing this.. u might want to turn off ur header and footer controls if u don't need them. That is also easy to do, just before calling RenderControl, make those controls visibility to hidden.

The main key here is the this.RenderControl which takes a HtmlTextWriter.

Hope this helps.

Thanks..
 
Share this answer
 
v2
Comments
Divya Naidu 12-Oct-11 0:36am    
Thank you so much for your help on this topic sazzad.Can you please provide me with the complete code that would be really helpful for me.Thanks again for your Reply.
Sazzad Hossain 12-Oct-11 1:48am    
this.RenderControl(ht); here this means the aspx page u r in....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900