Hi mahesh213,
Check this example. Now please take its reference and correct your code.
Namespaces
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View();
}
public JsonResult getAll()
{
return Json(GetEmployees(), JsonRequestBehavior.AllowGet);
}
[HttpPost]
public ActionResult GenerateReport(int eId)
{
string html = "";
Employee employee = GetEmployees().Where(x => x.EId == eId).FirstOrDefault();
// Company Logo and Name.
html += "<table>" +
"<tr><td colspan='2'><img src='" + GetUrl("Files/Logo.png") + "' height='50px' width='100px' /></td></tr>" +
"<tr><td>Company : </td><td><b>Excelasoft Solutions</b></td></tr>" +
"<tr><td colspan='2'> </td></tr></table>";
// Employee Data.
html += "<table border='1' cellpadding='0' cellspacing='0' width='100%'><tr>" +
"<th>Id</th>" +
"<th>Name</th>" +
"<th>Gender</th>" +
"<th>Phone No</th>" +
"<th>Location</th>" +
"<th>Company</th>" +
"</tr><tr>" +
"<td>" + employee.EId + "</td>" +
"<td>" + employee.EName + "</td>" +
"<td>" + employee.Gender + "</td>" +
"<td>" + employee.PNo + "</td>" +
"<td>" + employee.Location + "</td>" +
"<td>" + employee.Company + "</td>" +
"</tr></table>";
byte[] bytes;
using (MemoryStream stream = new MemoryStream())
{
StringReader sr = new StringReader(html);
Document pdfDoc = new Document(PageSize.A4, 5f, 5f, 10f, 5f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
bytes = stream.ToArray();
}
Bitmap bitmap = new Bitmap(300, 100, PixelFormat.Format64bppArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.White);
graphics.DrawString("@ASPSnippets.com", new System.Drawing.Font("Arial", 20, FontStyle.Bold), new SolidBrush(Color.Red), new PointF(0.4F, 2.4F));
bitmap.Save(Server.MapPath("~/Image.jpg"), ImageFormat.Jpeg);
bitmap.Dispose();
var img = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Image.jpg"));
img.SetAbsolutePosition(200, 600);
PdfContentByte waterMark;
using (MemoryStream stream = new MemoryStream())
{
PdfReader reader = new PdfReader(bytes);
using (PdfStamper stamper = new PdfStamper(reader, stream))
{
int pages = reader.NumberOfPages;
for (int i = 1; i <= pages; i++)
{
waterMark = stamper.GetUnderContent(i);
waterMark.AddImage(img);
}
}
bytes = stream.ToArray();
}
System.IO.File.Delete(Server.MapPath("~/Image.jpg"));
TempData["Data"] = bytes;
return new JsonResult() { Data = new { FileName = employee.EName.Trim().Replace(" ", "_") + ".pdf" } };
}
[HttpGet]
public virtual ActionResult Download(string filename)
{
if (TempData["Data"] != null)
{
byte[] data = TempData["Data"] as byte[];
return File(data, "application/pdf", filename);
}
else
{
return new EmptyResult();
}
}
private string GetUrl(string imagePath)
{
string appUrl = System.Web.HttpRuntime.AppDomainAppVirtualPath;
if (appUrl != "/")
{
appUrl = "/" + appUrl;
}
string url = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, appUrl);
return url + imagePath;
}
private List<Employee> GetEmployees()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee { EId = 1, EName = "Maria", Gender = "Female", PNo = "030-0074321", Location = "Austria", Company = "Alfreds Futterkiste" });
employees.Add(new Employee { EId = 2, EName = "Antonio Moreno", Gender = "Male", PNo = "(5) 555-3932", Location = "Brazil", Company = "Antonio Moreno Taquería" });
employees.Add(new Employee { EId = 3, EName = "Thomas Hardy ", Gender = "Male", PNo = "(5) 555-3932", Location = "Ireland", Company = "Around the Horn" });
return employees;
}
public class Employee
{
public int EId { get; set; }
public string EName { get; set; }
public string Gender { get; set; }
public string PNo { get; set; }
public string Location { get; set; }
public string Company { get; set; }
}
}
View
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Index</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', []);
app.controller('MyController', ['$scope', '$http', function ($scope, $http) {
$http({
method: 'GET',
url: '/Home/getAll/'
}).success(function (data) {
$scope.items = data;
});
$scope.GenerateReport = function (id) {
$http({
method: 'POST',
url: '/Home/GenerateReport/',
params: { eId: id }
}).success(function (data) {
window.location = '/Home/Download?filename=' + data.FileName;
});
};
} ]);
</script>
</head>
<body ng-app="MyApp" ng-controller="MyController">
<div class="container" id="printarea">
<table class="table table-bordered">
<tr class="success">
<th>EId</th>
<th>EName</th>
<th>Gender</th>
<th>PNo</th>
<th>Location</th>
<th>Company</th>
<th>Ation</th>
</tr>
<tr ng-repeat="item in items">
<td>{{item.EId}}</td>
<td>{{item.EName}}</td>
<td>{{item.Gender}}</td>
<td>{{item.PNo}}</td>
<td>{{item.Location}}</td>
<td>{{item.Company}}</td>
<td>
<button class="btn btn-success btn-sm" ng-click="GenerateReport(item.EId)">
<span class="glyphicon glyphicon-print"></span> PDF
</button>
</td>
</tr>
</table>
</div>
</body>
</html>
Screenshot