将网页转换为pdf并作为附件通过asp.net c#中的电子邮件发送 [英] Convert web page to pdf and send it as attachment via email in asp.net c#

查看:51
本文介绍了将网页转换为pdf并作为附件通过asp.net c#中的电子邮件发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的aspx页面包含文本框.我希望pdf文件在文本框中输入所有值.我可以使用iTextSharp转换为pdf,但文本框值未保存.让我知道您是否有这样做的方法.
这是我正在使用的代码.

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.

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) { }
        }

推荐答案

嗨 不要使用Response.OutputStream,而是使用memoryStream,因为在Response.End()
关闭输出流,并处理在输出流中创建的pdf.
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.


这篇关于将网页转换为pdf并作为附件通过asp.net c#中的电子邮件发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆