Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My aspx page contains text boxes. i want the pdf file to have all the values entered in the text box. I was able to convert to pdf using iTextSharp but the text box values are not getting saved. Let me know if you have any approach to do this.
Here is the code am using.

C#
protected void SendMail()
        {
            var userName = "anusha-4.n-4@cognizant.com";
           
            var toAddress = YourEmail.Text.ToString();
            
            const string Password = "Mypassword123#";
          
            string subject = YourSubject.Text.ToString();
            string body = "From: " + YourName.Text + "\n";
            body += "Email: " + YourEmail.Text + "\n";
            body += "Subject: " + YourSubject.Text + "\n";
            body += "Question: \n" + Comments.Text + "\n";
        
           
            var smtp = new System.Net.Mail.SmtpClient();
            {
                smtp.Host = "10.238.52.200";
                smtp.Port = 25;
                smtp.EnableSsl = false;
                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtp.Credentials = new NetworkCredential(userName, Password);
                smtp.Timeout = 20000;
            }
            smtp.Send(userName, toAddress, subject, body);
        }
         protected void Button1_Click(object sender, EventArgs e)
        {
            Response.ContentType = "application/pdf"; 
            Response.AddHeader("content-disposition", "attachment;filename=TestPage.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, 100f, 0f); 
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream); 
            pdfDoc.Open(); 
            htmlparser.Parse(sr); 
           
            Response.Write(pdfDoc);
           
             try
            {
                 SendMail();
               
                DisplayMessage.Text = "Your Comments after sending the mail";
                DisplayMessage.Visible = true;
                YourSubject.Text = "";
                YourEmail.Text = "";
                YourName.Text = "";
                Comments.Text = "";
                pdfDoc.Close();
                Response.End();
            }
            catch (Exception) { }
        }
Posted

1 solution

Hi Instead of using Response.OutputStream use memoryStream because on Response.End()
the output stream is closed and created pdf in output stream is disposed.
 
Share this answer
 

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