Friday, 21 November 2014

Create/Generat PDF with image Using RazorPDF in ASP.Net MVC

Hi friends,in this article I will explain about How to Create/Generat PDF with image Using RazorPDF in ASP.Net MVC 4 Razor.
I already explained in the previous articles about Export Data from Database Table to Excel File in MVC ASP.NET,MVC4: Show Multiple Models in a Single View using dynamically created object and MVC Jquery UI accordion expand/collaspe all in ASP.NET
We will create a simple Users example to see the RazorPDF package. Let's create a ASP.NET MVC 4 project.

Install RazorPdf using NuGet Package Manager:
Right-click on the References node in Solution Explorer, Click on "Manage NuGet Packages". This brings up the NuGet Manage Package dialog box and then search RazorPDF in search box and click on install to install the RazorPDF package as shown below figure.


Model(User.cs):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCRazorPDF.Models
{
    public class User
    {
        public int UserID { get; set; }
        public string UserName { get; set; }
        public string Address { get; set; }
        public string Subject { get; set; }
    }
}

Controller(UserController.cs):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCRazorPDF.Models;
namespace MVCRazorPDF.Controllers
{
    public class UserController : Controller
    {
        public ActionResult Index()
        {
            var userDetails = new List<User>()
                {
                    new User() {UserID = 101, Subject = "C#", UserName = "Kishore", Address = "XXXXXXXX"},
                    new User() {UserID = 102, Subject = "asp.net", UserName = "Satyam", Address = "XXXXXXXX"},
                    new User() {UserID = 103, Subject = "MVC", UserName = "Raghava", Address = "XXXXXXXX"},
                    new User() {UserID = 104, Subject = "SQL Server", UserName = "SambhaShiva", Address = "XXXXXXXX"},
                };

            return new RazorPDF.PdfResult(userDetails, "Index");
        }

    }
}
View(Index.cshtml):
@model IEnumerable<MVCRazorPDF.Models.User>
@{
    ViewBag.Title = "Index";
}
<!DOCTYPE html>
<html>
<body>
    <h2 align="center">
        User Details</h2>
    <table width="100%" cellpadding="0" cellspacing="10" widths="25;25;25;25" borderwidth="1.0">
    <tr> <td colspan="4" align="center">
                <img src="@Server.MapPath("~/Images/Multiple-user.png")" width="100" height="100"/>
            </td></tr>
        <tr>
            <th bgcolor="#ff6699" align="center" valign="top">
                @Html.DisplayNameFor(model => model.UserID)
            </th>
            <th bgcolor="#ff6699" align="center" valign="top">
                @Html.DisplayNameFor(model => model.UserName)
            </th>
            <th bgcolor="#ff6699" align="center" valign="top">
                @Html.DisplayNameFor(model => model.Address)
            </th>
            <th bgcolor="#ff6699" align="center" valign="top">
                @Html.DisplayNameFor(model => model.Subject)
            </th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.UserID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.UserName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Address)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Subject)
                </td>
            </tr>
        }
    </table>
</body>
</html>



The output of the above code as shown in the below figure.

You can download the code by clicking on the below Download image. 

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.