Generate PDF file Using Entity Framework in Asp.Net
1> Create a Table name Videos
CREATE TABLE [dbo].[Videos] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Type] INT NULL,
[VideoUrl] VARCHAR (MAX) NULL,
[Status] BIT NULL,
CONSTRAINT [PK_Videos] PRIMARY KEY CLUSTERED ([Id] ASC)
);
2> Create Model of Videos Table
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace ItsMe.Areas.Admin.Models
{
[Table("Videos")]
public class Videos
{
public int Id { get; set; }
public int Type { get; set; }
public string VideoUrl { get; set; }
public bool Status { get; set; }
}
}
3> Add a itextsharp.dll file to the references.
write the comnand for install itextsharp
4> PM > Install-Package iTextSharp
5> Create a Controller write an Index action so that all data list in Index view.
{
return View(_typesServices.GetAll()
}
5> Now Create Action for PrintPdf in controller
{
var model = new Types();
var type = _typesServices.GetById(id);
model.Id = type.Id;
model.Name = type.Name;
model.Status = type.Status;
Document pdfDoc = new Document(PageSize.A4, 25, 10, 25, 10);
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
Paragraph Text = new Paragraph(
"<br/>"+"Id" +model.Id+"<br/>"+model.Name+"<br/>"+model.Status
);
pdfDoc.Add(Text);
pdfWriter.CloseStream = false;
pdfDoc.Close();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Example.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
return RedirectToAction("Index");
}
6> Add the new link right side of detail in Index form.
7> Run the Progtam an click the PrintPdf for generate PDF file.