TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Export PDF from Grid in MVC
Mukesh Kumar
Nov 19
2015
Code
992
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
System.Net.WebClient webClient=
new
System.Net.WebClient();
//passing url of local web page to read its html content
Stream responseData = webClient.OpenRead(
"http://localhost:51951/Test/Default4.aspx"
);
//converting stream into stream reader object
StreamReader inputstream =
new
StreamReader(responseData);
//If you want to read text from other source like plain text file or user input, ignore all above lines
StringWriter sw =
new
StringWriter();
HtmlTextWriter writer =
new
HtmlTextWriter(sw);
writer.Write(inputstream.ReadToEnd());
//comment above line and uncomment below line if you wish to convert text file to pdf
//writer.Write(File.ReadAllText(@"E:\MyFolder\filename.txt"));
StringReader sr =
new
StringReader(sw.ToString());
//Parse into IElement
List<IElement> elements =HTMLWorker.ParseToList(sr,
null
);
//Open the Document
itextDoc.Open();
//loop through all elements
foreach
(IElement el
in
elements)
{
//add individual element to Document
itextDoc.Add(el);
}
//Close the document
itextDoc.Close();
//set the Response object
Response.ContentType =
"application/pdf"
;
Response.AddHeader(
"content-disposition"
,
"attachment;filename=TestPage.pdf"
);
Response.End();
mvc
ASP.NET