Generate pdf in MVC



  • Download itextsharp from here  and add reference to your project
  • OR add it via Nuget package manager
  • Add class(PdfUtil) to your project  should look like this


 sing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.Drawing;
namespace ExportToExcel.Domain
{
public class PdfUtil
{
public Paragraph AddParagragh(string ParagraphText)
{
Paragraph p = new Paragraph();
p.SpacingBefore = 10f;
p.FirstLineIndent = 10f;
p.SpacingAfter = 15f;

iTextSharp.text.Font f = new iTextSharp.text.Font();
p.Font.SetFamily("Courier");
p.Alignment = Element.ALIGN_JUSTIFIED;
p.Font.Size = 13f;
p.Font.SetColor(0, 0, 0);
p.Add(ParagraphText);
return p;
}
public Chunk AddParagraphHeader(string headingText)
{
Chunk ch = new Chunk(headingText);
ch.Font.Size = 16f;
ch.Font.SetStyle("bold");
ch.Font.SetColor(0, 0, 0);
return ch;
}
}
}
 
Add Controller to your project(PdfGenerationController) should look like this


 using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ExportToExcel.Domain;
namespace ExportToExcel.Controllers
{
public class PdfGenerationController : Controller
{
// GET: PdfGeneration
public ActionResult GeneratePdf()
{
return View();
}
[HttpPost]
public ActionResult GeneratePdf(string heading,string paragraph)
{
PdfUtil objPdf = new PdfUtil();

string path = Server.MapPath("~/Reports/myfile.pdf");
if (!Directory.Exists(Server.MapPath("~/Reports")))
Directory.CreateDirectory(Server.MapPath("~/Reports"));


iTextSharp.text.Rectangle pagesize = new iTextSharp.text.Rectangle(20, 20, PageSize.A4.Width, PageSize.A4.Height);
Document doc = new Document(pagesize, 10, 10, 50, 10);
MemoryStream ms = new MemoryStream();
PdfWriter pw = PdfWriter.GetInstance(doc, ms);
doc.Open();

// System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/Reports/mvc.png"));
Image logo = Image.GetInstance(Server.MapPath("~/Reports/mvc.png"));
logo.Alignment = Element.ALIGN_MIDDLE;
logo.ScaleAbsolute(150, 70);
doc.Add(logo);

doc.Add(objPdf.AddParagraphHeader(heading));
doc.Add(objPdf.AddParagragh(paragraph));

PdfPTable tbl = new PdfPTable(4);
tbl.AddCell("Sr No");
tbl.AddCell("Name");
tbl.AddCell("Address");
tbl.AddCell("Phone");
for (int i = 1; i < 6; i++)
{
tbl.AddCell(i.ToString());
tbl.AddCell("Name " + i.ToString());
tbl.AddCell("Address " + i.ToString());
tbl.AddCell("6789655" + i.ToString());
}
doc.Add(tbl);

doc.Close();
byte[] byteArray = ms.ToArray();
ms.Flush();
ms.Close();
ms.Dispose();
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=myfile.pdf");
Response.AddHeader("Content-Length", byteArray.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(byteArray);
return View();
}
}
}
 
Add view to (GeneratePdf) action method



The view should look like this


 @{
    ViewBag.Title = "GeneratePdf";
Layout = "~/Views/Shared/_MyLayout.cshtml";
}

<h2>Generate Pdf</h2>

@using (Html.BeginForm())
{
<hr />
<div class="form-horizontal">
<div class="form-group">
<label class="col-md-2 control-label">Heading</label>
<div class="col-md-6">
<input type="text" class="form-control" id="heading" name="heading" />
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Paragraph</label>
<div class="col-md-10">
<textarea id="paragraph" name="paragraph" class="form-control" cols="120" rows="10"></textarea>
</div>
</div>

<div class="form-group">

<div class=" col-md-offset-2 col-md-2">
<button type="submit" class="btn btn-success" value="Generate">Generate</button>
</div>
</div>
</div>
}
Create folder in your project named (Reports)
add image (logo) to this folder in my case it is (mvc.png) add your own and change the name in code.
Now run the application Enter heading Text & paragraph text and generated pdf.

3 Comments

  1. Truely a very good article on how to handle the future technology. After reading your post,thanks for taking the time to discuss this, I feel happy about and I love learning more about this topic.keep sharing your information regularly for my future reference

    Software Testing Training in Chennai

    Web Designing Training in Chennai

    ReplyDelete
  2. simply superb,mind blowing, i will share your blog to my friends also
    .NET Online Course Hyderabad

    ReplyDelete

Post a Comment