关于c#:iTextSharp使用Memorystream将PDF作为附件发送

iTextSharp send PDF as attachment using Memorystream

一直在挣扎,已经走得这么近了,但还不完全在那里。我正在尝试使用ITextSharp 4.1.2版以电子邮件附件的形式发送PDF。下面我提供的代码同时使用了filestream和memorystream,filestream是为了向我自己证明PDF模板上有数据(它是)。另外,我正在接收电子邮件,但没有附件。有人能看看我遗漏了什么吗?

1
public class BasePDFController : BaseController

{受保护的actionresult emailpdf(对象模型){字符串pdf=path.combine(server.mappath("~/infrastructure/pdfemplates/fw9.pdf"));string outputfilepath=@"c:projects emp estu templateu filled.pdf";

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    MemoryStream memoryStream = new MemoryStream();

    PdfReader pdfFileReader = null;
    PdfReader pdfMemoryReader = null;

    try
    {
        pdfFileReader = new PdfReader(pdf);
        pdfMemoryReader = new PdfReader(pdf);

        using (FileStream pdfOutputFile = new FileStream(outputFilePath, FileMode.Create))
        {
            PdfStamper pdfFileStamper = null;
            PdfStamper pdfMemoryStamper = null;
            try
            {
                pdfFileStamper = new PdfStamper(pdfFileReader, pdfOutputFile);
                pdfMemoryStamper = new PdfStamper(pdfMemoryReader, memoryStream);

                AcroFields acroFileFields = pdfFileStamper.AcroFields;
                AcroFields acroMemoryFields = pdfMemoryStamper.AcroFields;

                acroFileFields.SetField("topmostSubform[0].Page1[0].f1_01_0_[0]","Batman");

                pdfFileStamper.FormFlattening = true;
                pdfMemoryStamper.FormFlattening = true;
                pdfMemoryStamper.Writer.CloseStream = false;
                if (pdfMemoryStamper != null)
                {
                    pdfMemoryStamper.Close();
                }
                memoryStream.Position = 0;

                EmailProvider.Email email = new EmailProvider.Email();
                email = new EmailProvider.Email
                {
                    To ="[email protected]",
                    Subject ="Scholars Attached PDF",
                    Body ="A PDF!",
                    Attachment = new Attachment(memoryStream, new System.Net.Mime.ContentType("application/pdf"))
                };
                EmailProvider.SendEmail(email);                    
                }
            finally
            {
                if (pdfFileStamper != null)
                {
                    pdfFileStamper.Close();
                }
            }
        }
    }
    finally
    {
        pdfFileReader.Close();
        pdfMemoryReader.Close();
    }
    return File(outputFilePath,"application/pdf","Returned.pdf");
}

}

编辑:-是的,邮件提供商有问题。但我不知道如何修复它,或者这是否是最好的发送方式?写这篇文章的人显然放弃了。编辑修复了附件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    public static class EmailProvider
{
    public class Email
    {
        public String To { get; set; }

        public String Subject { get; set; }

        public String Body { get; set; }

        **public Attachment Attachment { get; set; }**
    }

    public static void SendEmail(Email email)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(email.To);
        mail.Subject = email.Subject;
        mail.Body = email.Body;
        mail.IsBodyHtml = true;
        mail.Attachments.Add(email.Attachment);
        SmtpClient smtp = new SmtpClient();

        smtp.Send(mail);
    }
}

电子邮件已修复,我正在接收PDF!但是,当我尝试从电子邮件中打开PDF时,会收到一个错误:打开此文档时出错。文件已损坏,无法修复。思想?


你应该在pdfMemoryStamper.Writer.CloseStream = false;之后打电话给pdfMemoryStamper.Close()

这样地:

1
2
3
4
5
6
7
8
9
10
11
// *snip*

pdfFileStamper.FormFlattening = true;
pdfMemoryStamper.FormFlattening = true;
pdfMemoryStamper.Writer.CloseStream = false;

pdfMemoryStamper.Close();

memoryStream.Position = 0;

// *snip*