In this blog, I will explain how to convert HTML documents to pdf, word, and excel. Here I am using itextSharp to convert the document.
First of all, we need to install itextshap and itextsharp.xmlworker from nuget package manager.
step1: Install ItextSharp and ItextSharp.xmlWorker
step2: In Html Page write the below code: for generating pdf file
<div id="Grid"> //Your html content </div> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> $(function () { $("#btnSubmit").click(function () { $("input[name='GridHtml']").val($("#Grid").html()); }); }); </script>
step 3: Write below code in Controller
[HttpPost] [ValidateInput(false)] public FileResult Pdf(string Html) { using (MemoryStream stream = new System.IO.MemoryStream()) { StringReader sr = new StringReader(Html); Document Doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f); PdfWriter writer = PdfWriter.GetInstance(Doc, stream); Doc.Open(); XMLWorkerHelper.GetInstance().ParseXHtml(writer, Doc, sr); Doc.Close(); return File(stream.ToArray(), "application", "File.pdf"); } }
step 4: we can also do the same thing for converting HTML documents to Word and Excel.
@using (Html.BeginForm("Word", "Students", FormMethod.Post)) { <input type="hidden" name="ExportWord" /> <input type="submit" id="btnWord" value="Word" /> } @using (Html.BeginForm("Excel", "Students", FormMethod.Post)) { <input type="hidden" name="ExportExcel" /> <input type="submit" id="btnExcel" value="Excel" /> } <div id="Grid"> //Your html content </div> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> $(function () { $("#btnWord").click(function () { $("input[name='GridHtml']").val($("#Grid").html()); }); }); $(function () { $("#btnExcel").click(function () { $("input[name='GridHtml']").val($("#Grid").html()); }); }); </script>
step 5: In Controller
[HttpPost] [ValidateInput(false)] public EmptyResult Word(string ExportWord) { Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=file.doc"); Response.Charset = ""; Response.ContentType = "application/msword"; Response.Output.Write(ExportWord); Response.Flush(); Response.End(); return new EmptyResult(); } [HttpPost] [ValidateInput(false)] public EmptyResult Excel(string ExportExcel) { Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=ExcelExport.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel"; Response.Output.Write(ExportExcel); Response.Flush(); Response.End(); return new EmptyResult(); }
step 6: Now Run the Project and See the Output.
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular