How to create PDF document in ASP.NET with C# using iTextSharp
In this blog I am going to explain how to create PDF document in ASP.NET using iTextSharp. First we have to download iTextSharp.dll class library and include to our project.
You can download iTextSharp.dll class library here::
http://sourceforge.net/projects/itextsharp/
<asp:Image ID="Image1" runat="server" Width="100%"
ImageUrl="https://www.google.co.in/images/srpr/logo11w.png"
Height="100%" />
</div>
<div>
<asp:GridView ID="gvDetails" runat="server" AlternatingRowStyle-BackColor="#FFFFCC">
</asp:GridView>
</div>
<div>
<asp:Button ID="btnGeneratePDF" runat="server" onclick="btnGeneratePDF_Click"
Text="Generate PDF" />
</div>
using System.Drawing;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected void BindData()
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId", typeof(Int32));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Designation", typeof(string));
dt.Columns.Add("Department", typeof(string));
DataRow dr = dt.NewRow();
dr["UserId"] = 101;
dr["Name"] = "Zaheer";
dr["Designation"] = "Associate Technical Consultant";
dr["Department"] = null;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = 12;
dr["Name"] = "Anish";
dr["Designation"] = "CRM";
dr["Department"] = "Associate Technical Consultant";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = 113;
dr["Name"] = "Manish";
dr["Designation"] = "Tech Lead";
dr["Department"] = "Navision";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = 4;
dr["Name"] = "Ravish";
dr["Designation"] = "Tech Lead";
dr["Department"] = "CRM";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = 19;
dr["Name"] = "Rajnsh";
dr["Designation"] = "Tech Lead";
dr["Department"] = "CRM";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = 103;
dr["Name"] = "Santosh";
dr["Designation"] = null;
dr["Department"] = "Navision";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = 44;
dr["Name"] = "Rakesh";
dr["Designation"] = "Tech Lead";
dr["Department"] = "Navision";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = 22;
dr["Name"] = "Vinay";
dr["Designation"] = "Associate Technical Consultant";
dr["Department"] = "Navision";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = 73;
dr["Name"] = "Manish";
dr["Designation"] = "Tech Lead";
dr["Department"] = "CRM";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = 27;
dr["Name"] = "Arnav";
dr["Designation"] = "Associate Technical Consultant";
dr["Department"] = "CRM";
dt.Rows.Add(dr);
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (string.IsNullOrEmpty(dt.Rows[i][j].ToString()))
{
dt.Rows[i][j] = "Data Not Available";
}
}
}
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void btnGeneratePDF_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Employee.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfdoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
HTMLWorker htmlparser = new HTMLWorker(pdfdoc);
PdfWriter.GetInstance(pdfdoc, Response.OutputStream);
pdfdoc.Open();
htmlparser.Parse(sr);
pdfdoc.Close();
Response.Write(pdfdoc);
Response.Flush();
Response.End();
}
You can download iTextSharp.dll class library here::
http://sourceforge.net/projects/itextsharp/
- Then open your project and include that iTextSharp.dll class library.
- In Designer Source page just add a Gridview and button. Here i have added a button for generating pdf, an image source and a Gridview for storing data ::
<asp:Image ID="Image1" runat="server" Width="100%"
ImageUrl="https://www.google.co.in/images/srpr/logo11w.png"
Height="100%" />
</div>
<div>
<asp:GridView ID="gvDetails" runat="server" AlternatingRowStyle-BackColor="#FFFFCC">
</asp:GridView>
</div>
<div>
<asp:Button ID="btnGeneratePDF" runat="server" onclick="btnGeneratePDF_Click"
Text="Generate PDF" />
</div>
- Now, add below code in .cs page ::
using System.Drawing;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected void BindData()
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId", typeof(Int32));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Designation", typeof(string));
dt.Columns.Add("Department", typeof(string));
DataRow dr = dt.NewRow();
dr["UserId"] = 101;
dr["Name"] = "Zaheer";
dr["Designation"] = "Associate Technical Consultant";
dr["Department"] = null;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = 12;
dr["Name"] = "Anish";
dr["Designation"] = "CRM";
dr["Department"] = "Associate Technical Consultant";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = 113;
dr["Name"] = "Manish";
dr["Designation"] = "Tech Lead";
dr["Department"] = "Navision";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = 4;
dr["Name"] = "Ravish";
dr["Designation"] = "Tech Lead";
dr["Department"] = "CRM";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = 19;
dr["Name"] = "Rajnsh";
dr["Designation"] = "Tech Lead";
dr["Department"] = "CRM";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = 103;
dr["Name"] = "Santosh";
dr["Designation"] = null;
dr["Department"] = "Navision";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = 44;
dr["Name"] = "Rakesh";
dr["Designation"] = "Tech Lead";
dr["Department"] = "Navision";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = 22;
dr["Name"] = "Vinay";
dr["Designation"] = "Associate Technical Consultant";
dr["Department"] = "Navision";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = 73;
dr["Name"] = "Manish";
dr["Designation"] = "Tech Lead";
dr["Department"] = "CRM";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = 27;
dr["Name"] = "Arnav";
dr["Designation"] = "Associate Technical Consultant";
dr["Department"] = "CRM";
dt.Rows.Add(dr);
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (string.IsNullOrEmpty(dt.Rows[i][j].ToString()))
{
dt.Rows[i][j] = "Data Not Available";
}
}
}
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void btnGeneratePDF_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Employee.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfdoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
HTMLWorker htmlparser = new HTMLWorker(pdfdoc);
PdfWriter.GetInstance(pdfdoc, Response.OutputStream);
pdfdoc.Open();
htmlparser.Parse(sr);
pdfdoc.Close();
Response.Write(pdfdoc);
Response.Flush();
Response.End();
}
Comments
Post a Comment