I want to store the Registration details into database & after storing, I want to display same details in pdf document so that the end user can download or take a print of it. the pdf should be open as a pop up or in new browser window..
Code for generating pdf is as follows:
protected void GenerateReport(object sender, EventArgs e)
{
MemberInfoSelected objSelectedMember = new MemberInfoSelected();
objSelectedMember.UserName = email.Text.Trim();
SqlDataReader dr = objSelectedMember.UserNameSelect(out ErrorMessage);
dr.Read();
Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK);
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
PdfTemplate template = new PdfTemplate();
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
Phrase phrase = null;
PdfPCell cell = null;
PdfPTable table = null;
Color color = null;
document.Open();
table = new PdfPTable(2);
table.TotalWidth = 500f;
table.LockedWidth = true;
table.SetWidths(new float[] { 0.3f, 0.7f });
cell = ImageCell("~/images/title_tag.gif", 30f, PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
table.AddCell(cell);
document.Add(table);
table = new PdfPTable(2);
table.TotalWidth = 500f;
table.LockedWidth = true;
table.SetWidths(new float[] { 0.3f, 0.7f });
cell = ImageCell("~/images/dhwajLogo.gif", 30f, PdfPCell.ALIGN_LEFT);
table.AddCell(cell);
cell = ImageCell("~/images/Name_New.gif", 40f, PdfPCell.ALIGN_JUSTIFIED);
cell.Colspan = 2;
table.AddCell(cell);
document.Add(table);
table = new PdfPTable(2);
table.TotalWidth = 500f;
table.LockedWidth = true;
table.SetWidths(new float[] { 0.3f, 0.7f });
cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
cell.VerticalAlignment = PdfCell.ALIGN_TOP;
cell.Colspan = 2;
table.AddCell(cell);
document.Add(table);
color = new Color(System.Drawing.ColorTranslator.FromHtml("#da251c"));
DrawLine(writer, 25f, document.Top - 130f, document.PageSize.Width - 25f, document.Top - 130f, color);
table = new PdfPTable(2);
table.HorizontalAlignment = Element.ALIGN_LEFT;
table.SetWidths(new float[] { 0.3f, 1f });
table.SpacingBefore = 30f;
cell = PhraseCell(new Phrase("Registration Form", FontFactory.GetFont("Arial",12,Font.UNDERLINE,Color.BLACK)), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
table.AddCell(cell);
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 30f;
table.AddCell(cell);
DrawRectAngle(writer, color);
phrase = new Phrase();
phrase.Add(new Chunk("NAME -: " + dr["FirstName"] + " " + dr["MiddleName"] + " " + dr["LastName"] + "\n", FontFactory.GetFont("Arial", 10, Font.BOLD, Color.BLACK)));
phrase.Add(new Chunk("NAME (in Marathi) -: " + dr["MarathiFirstName"] + " " + dr["MarathiMiddleName"] + " " + dr["MarathiLastName"] + "\n", FontFactory.GetFont("Mangal",10, Font.BOLD, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.Colspan = 2;
cell.VerticalAlignment = PdfPCell.ALIGN_LEFT;
table.AddCell(cell);
document.Add(table);
table = new PdfPTable(2);
table.SetWidths(new float[] { 0.5f, 2f });
table.TotalWidth = 340f;
table.LockedWidth = true;
table.SpacingBefore = 20f;
table.HorizontalAlignment = Element.ALIGN_RIGHT;
table.AddCell(PhraseCell(new Phrase("Date of Birth:", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase(Convert.ToDateTime(dr["DateOfBirth"]).ToString("dd MMMM, yyyy")+"\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
table.AddCell(PhraseCell(new Phrase("Gender:", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
if (dr["Male"].Equals(true))
table.AddCell(PhraseCell(new Phrase("Male \n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT));
else
table.AddCell(PhraseCell(new Phrase("Female \n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
table.AddCell(PhraseCell(new Phrase("Marital Status:", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase(dr["MaritalStatus"]+"\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
table.AddCell(PhraseCell(new Phrase("Education:", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase(dr["Education"]+"\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
table.AddCell(PhraseCell(new Phrase("Occupation:", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase(dr["Occupation"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
table.AddCell(PhraseCell(new Phrase("Email:", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase(dr["Email"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
table.AddCell(PhraseCell(new Phrase("Address:", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
phrase = new Phrase(new Chunk(dr["Address"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
document.Add(table);
document.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
Response.Clear();
Response.ContentType= "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" + dr["FirstName"] + " " + dr["LastName"] + "-Registration Form.pdf");
Response.ContentType= "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
}
}