Hi,
Check this code, It will work according to your requirement.
The "PdfPTable" object is instantiated as a five column table then use "AddCell" to add header and values.
public void pdf()
{
Employees emp = new Employees();
MemoryStream workStream = new MemoryStream();
Document document = new Document();
PdfWriter.GetInstance(document, workStream).CloseStream = false;
document.Open();
document.Add(new Paragraph("msg"));
document.Add(new Paragraph(DateTime.Now.ToString()));
PdfPTable table = new PdfPTable(5);
float[] widths = new float[] { 100f, 80f, 100f, 90f, 130f };
table.SetWidths(widths);
table.AddCell("Employee Name");
table.AddCell("Experience in Months");
table.AddCell("Band");
table.AddCell("Designation");
table.AddCell("Current Project");
table.AddCell(emp.Name);
table.AddCell(emp.Experience.ToString());
table.AddCell(emp.Band);
table.AddCell(emp.Designation);
table.AddCell(emp.Project);
document.Add(table);
document.Close();
byte[] byteInfo = workStream.ToArray();
workStream.Write(byteInfo, 0, byteInfo.Length);
workStream.Position = 0;
Response.Buffer = true;
Response.AddHeader("Content-Disposition", "attachment; filename= " + Server.HtmlEncode("doc.pdf"));
Response.ContentType = "APPLICATION/pdf";
Response.BinaryWrite(byteInfo);
}
This is the class where I am retrieving data from database. Here I have done hard coding if you want you can get data from database using Sqlconnection and all.
public class Employees
{
public string Name = "Smith";
public int Experience = 0;
public string Band = "B4";
public string Designation = "Software Engineer";
public string Project = "XYZ";
}