Hi KatieNgoc,
Please refer below sample.
Text File
<h1>www.aspsnippets.com</h1>
Namespaces
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
using iTextSharp.text.html.simpleparser;
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
[ValidateInput(false)]
public FileResult Export()
{
using (MemoryStream stream = new MemoryStream())
{
string path = Server.MapPath("~/Data.txt");
string data = System.IO.File.ReadAllText(path);
StringReader sr = new StringReader(data);
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
return File(stream.ToArray(), "application/pdf", "Grid.pdf");
}
}
}
View
@model IEnumerable<Export_PDF_MVC.Customer>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Export", "Home", FormMethod.Post))
{
<input type="submit" id="btnSubmit" value="Export" />
}
</body>
</html>
Screenshot