单击提交时创建 PDF 和电子邮件

PDF creation and email on clicking submit

我有一个用于选择产品的复选框列表,还有一个用于输入数量的文本条目。单击提交后,我需要执行两个操作: 1)我想让客户选择并进入电子邮件(使用给定的电子邮件地址),并作为(创建)带有日期时间戳的 PDF 附件通过电子邮件发送。 2) 点击提交按钮后的下一页,应生成 next/another 页面,其中包含电子邮件确认消息和带有日期时间戳的 PDF 附件的打印按钮。 此网络表单中还有许多其他内容,但这是我需要一些指导和任何 link 或代码示例来帮助我找到合适的解决方案的领域,您可以在这里回复我,我们将不胜感激。

对于 PDF 创建,有几种工具 - 我使用过 iTextSharp。你可以在VS上下载nuget包,示例代码可以找到here。您可能希望使用表格来使它看起来更漂亮,但要习惯它,也许只需输入一些信息以了解它是如何工作的。

该方法可能如下所示:

public byte[] CreateOrderPdf(OrderDetails details)
{
    byte[] fileBytes;
    using (MemoryStream PdfStream = new MemoryStream())
    {
        iTextSharp.text.Document doc = new iTextSharp.text.Document();
        PdfWriter writer = PdfWriter.GetInstance(doc, PdfStream);

        doc.Open(); 
        doc.Add(new Paragraph(OrderDetails.Name));
        doc.Add(new Paragraph(OrderDetails.Address)); 
        //Add More Tables and Content Here (See Documentation for more)

        doc.Close();
        fileBytes = PdfStream.ToArray(); 
    }
    return fileBytes
}

对于电子邮件,.Net 具有 System.Net.Mail.MailMessage class。您可以将 pdf 作为流附加。您需要定义收件人、发件人、主题、正文等。看起来像这样:

string fileName = string.Format("OrderConfirmation-{0}.pdf", DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss")); 
MailMessage message = new MailMessage(from, to);
message.Subject = subject;
message.Body = body;

MemoryStream stream = new MemoryStream(fileBytes);    
stream.Position = 0; 
Attachment attachment = new Attachment(stream, fileName, MediaTypeNames.Application.Octet);
message.Attachments.Add(attachment);

SmtpClient mailServer = new SmtpClient(example.stmphost.com);
mailserver.Send(message);

message.Dispose(); 

希望这对您来说是一个好的起点。

在我寻找解决方案和建议起点的过程中,我偶然发现了这个,我也会提到这个,以供任何寻找相同答案的人在 www.e-iceblue 上查看 spire.pdf。 com/Introduce/pdf-for-net-introduce.html