Live Webinar: Prompt Engineering: Skill Everyone Must Learn Today
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Export PDF from Grid in MVC
WhatsApp
Mukesh Kumar
Nov 19
2015
1
k
0
0
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
Up Next
Export PDF from Grid in MVC