Click here to Skip to main content
15,885,947 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C#
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();

                //Header Table
                table = new PdfPTable(2);
                table.TotalWidth = 500f;
                table.LockedWidth = true;
                table.SetWidths(new float[] { 0.3f, 0.7f });

                //Company Logo
                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);
                

                //Separater Line
                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);

                //Name
                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;

                //Date of Birth
                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);                

                //Gender
                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);

                //Marital Status
                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);

                //Education
                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);

                //Occupation
                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);
                
                //Email
                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);

                //Address
                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();
            }
        }
Posted
Updated 26-May-14 10:44am
v4
Comments
ZurdoDev 26-May-14 21:32pm    
So, what's your question?
Sujit Keng 28-May-14 1:55am    
I want to show this pdf file as a popup or in a new browser tab instead of downloading it,how can I do it?

Right now the pdf directly get download after user submit the registration form..
[no name] 6-Jun-14 6:48am    
For what you're asking, Adobe PDF reader plugin should be installed in their browsers.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900