Create PDF Using RazorPDF in ASP.Net MVC

This article explains how to create a PDF in Razor View using RazorPDF in MVC. RazorDPF is a package that uses iTextSharp internally. 

We will create a simple marks card example to see the RazorPDF package. Let's create a ASP.NET MVC 4 project, as shown in Figure 1.1.
 
Select "ASP.NET MVC 4 Web Application" and click on the "OK" button. This brings up the dialog box to select the MVC templates.
 
Figure 1.1.JPG
 
Figure 1.1
 
Select Internet Application template and ensure that Razor is selected as the view engine, as shown in Figure 1.2.
 
Figure 1.2.JPG
 
Figure 1.2
 
Install RazorPdf using NuGet Package Manager
 
Right-click on the References node in Solution Explorer, as shown in Figure 1.3. Click on "Manage NuGet Packages". This brings up the NuGet Manage Package dialog box, that is used to install the RazorPDF package.

 
Figure 1.3.JPG
 
Figure 1.3
 
The NuGet dialog box lists installed packages by default, so click on the online tab, as shown in Figure 1.4.
 
Figure 1.4.JPG
 
Figure 1.4
 
Type "RazorPDF" in the search bar in the top-right side of NuGet. Once you find the RazorPDF, click on the install button to install the RazorPDF package in your project. Once installation is finished, automatically iTextSharp and RazorPDF will be added as a reference in your project, as shown in Figure 1.5.

Figure 1.5.JPG 
 
Figure 1.5
 
Create a Marks Card Model 
 
Create a class "MarksCard" to model the marks card.
 

namespace RazorPDFDemo.Models

{

   public classMarksCard

    {

       public int RollNo

        {

           get;

           set;

        }

       public string Subject

        {

           get;

           set;

        }

       public int FullMarks

        {

           get;

           set;

        }

       public int Obtained { get; set; }

    }

}
 

Once you have created the MarksCard class, build your project. So we can create the strongly typed view. 
 
Create Controller for StudentMarks
 
Right-click on the Controllers folder then select "Add" -> "Controller", as shown in Figure 1.6. 
 
Figure 1.6.JPG
 
Figure 1.6
 
The Controller dialog box will open, as shown in Figure 1.7. Change the controller name to Students and select "Empty API controller" in the Scaffolding template and click on the "Add" button.
 
Figure 1.7.JPG
 
Figure 1.7
 
We will create a list and store some data in the list. See the following code:
 

using System.Collections.Generic;

using System.Web.Mvc;

using RazorPDFDemo.Models;

namespace RazorPDFDemo.Controllers

{

   public classStudentsController : Controller

    {

        //// GET: /Students/public ActionResult Index()

    {

        var studentMarks = new List<MarksCard>()

        {

           new MarksCard()

            {

                RollNo = 101, Subject = "C#",

                FullMarks = 100, Obtained = 90},new MarksCard() {RollNo = 101, Subject = "asp.net", FullMarks = 100, Obtained = 80},new MarksCard() {RollNo =               101, Subject = "MVC", FullMarks = 100,

                Obtained = 100},new MarksCard() {RollNo = 101, Subject = "SQL Server", FullMarks = 100, Obtained = 75},

        };

       return new RazorPDF.PdfResult(studentMarks, "Index");

    }

}

}

Now our controller is ready. We need to create a view to display this marks card. Right-click on the Index() controller and click on "Add View". A view dialog box will be displayed, as shown in Figure 1.8. Make sure that Razor is selected in the view engine.
 

Select MarksCard in the Model class and click on the "Add" button.
 
Figure 1.8.JPG

Figure 1.8
 
The view will be created for the marks card. I have done some cosmetic changes in the view to make the look and feel like a marks card, see the following view code:

@model IEnumerable<RazorPDFDemo.Models.MarksCard>
@{Layout = null;
}
<!DOCTYPE html>
<html>
<body><table border="1" width='500' bordercolor="RED"><tr><td colspan="3" bgcolor="LightGreen" align="center" valign="top">SSLC Marks Sheet 2013</td></tr><tr><td>@{ var rollNumber = Model.Select(z => z.RollNo).Take(1).ToArray();}Riyaz Akhter<br />RollNo:@rollNumber[0]</td><td colspan="2" align="left"><img src="@Server.MapPath("~/Images/images.jpg")" width="100" height="100"/></td></tr><tr><td bgcolor="lightblue">@Html.DisplayNameFor(moel => moel.Subject)</td><td bgcolor="lightblue">@Html.DisplayNameFor(model => model.FullMarks)</td><td bgcolor="lightblue">@Html.DisplayNameFor(model => model.Obtained)</td></tr>@{int total = 0;}@foreach (var item in Model){<tr><td>@Html.DisplayFor(modelItem => item.Subject)</td><td>@Html.DisplayFor(modelItem => item.FullMarks)</td><td>@Html.DisplayFor(modelItem => item.Obtained)</td></tr>total += item.Obtained;}<tr><td></td><td><strong><font color="GREEN">Total</font></strong></td><td>@total</td></tr></table>
</body>
</html>
 
In the code above, I have changed the default layout to null and also I have added <html> and <body> tags.
 
Now your application is ready to run. Once you run the application it will look as in Figure 1.9.
 
Figure 1.9.JPG
 
Figure 1.9
 
Now we will convert this view to PDF. Go to your student controller and make a small change in your controller, as shown in Figure 1.10. 
 
Figure 1.10.JPG
 
Figure 1.10
 
Yes, that's it. pdfResult is a method in the RazorPDF class that takes a model object and view name as a parameter or you can pass a model object without an action name if your action name is a PDF.

Now run your application. This time the PDF will be displayed instead of your view, as shown in Figure 1.11.
 
Figure 1.11.JPG
 
Figure 1.11
 
It is very easy to create a PDF using RazorPDF in ASP.NET MVC.

Up Next
    Ebook Download
    View all
    Learn
    View all