Wednesday, 13 September 2017

Create (Generate) PDF file and Download in ASP.Net MVC.

Create (Generate) PDF file and Download in ASP.Net MVC

Description:


In this example we explain that how to create PDF file in asp.net MVC.or how to generate PDF file in asp.net MVC.or how to create PDF file and download functionality in MVC.here we use iTextSharp dll to create(Generate) and download PDF file in MVC.

Here we first bind or populate employee data in Table and then create (generate) PDF file and export it or download it using MVC.

Controller:

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
using iTextSharp.text.html.simpleparser;

public class EmployeeController : Controller
    {
        public ActionResult Index()
        {
            NorthwindEntities entities = new NorthwindEntities();
            return View(from Employee in entities.Employees.Take(10)
                        select Employee);
        }

        [HttpPost]
        [ValidateInput(false)]
        public FileResult Export(string GridHtml)
        {
            using (MemoryStream stream = new System.IO.MemoryStream())
            {
                StringReader sr = new StringReader(GridHtml);
                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.Employee>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Create (Generate) PDF file and Download in ASP.Net MVC</title>
    <style type="text/css">
        body {
            font-family: Arial;
            font-size: 9pt;
        }    
    </style>
</head>
<body>
    <h4>Employees</h4>
    <hr/>
    <div id="Grid">
        <table cellpadding="5" cellspacing="0" style="border: 1px solid #ccc;font-size: 9pt;">
            <tr>
                <th style="background-color: #B8DBFD;border: 1px solid #ccc">EmployeeID</th>
                <th style="background-color: #B8DBFD;border: 1px solid #ccc">ContactName</th>
                <th style="background-color: #B8DBFD;border: 1px solid #ccc">City</th>
                <th style="background-color: #B8DBFD;border: 1px solid #ccc">Country</th>
            </tr>
            @foreach (Employee Employee in Model)
            {
                <tr>
                    <td style="width:120px;border: 1px solid #ccc">@Employee.EmployeeID</td>
                    <td style="width:120px;border: 1px solid #ccc">@Employee.ContactName</td>
                    <td style="width:120px;border: 1px solid #ccc">@Employee.City</td>
                    <td style="width:120px;border: 1px solid #ccc">@Employee.Country</td>
                </tr>
            }
        </table>
    </div>
    <br/>
    <br/>
    @using (Html.BeginForm("Export", "Home", FormMethod.Post))
    {
        <input type="hidden" name="GridHtml"/>
        <input type="submit" id="btnSubmit" value="Export"/>
    }
    <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>
</body>
</html>




This entry was posted in :

0 comments:

Post a Comment