当Memorystream用作电子邮件附件时,ITextSharp正在输出空白文档

时间:2013-10-28 20:08:35

标签: .net vb.net itextsharp system.net.mail

执行以下代码时,不会抛出任何错误,但作为附件生成的pdf为空白且文件大小为0k。

编辑:

Dim page As Page = New Page()
Dim form As HtmlForm = New HtmlForm()
page.Controls.Add(form)

Dim bpa As UserControl_BpaReceipt = page.LoadControl("~/UserControl/BpaReceipt.ascx")
bpa._thisCart = _thisCart
form.Controls.Add(bpa)
Dim writer As StringWriter = New StringWriter()

Me.Server.Execute(page, writer, False)

结束编辑:

Dim bytes = Encoding.UTF8.GetBytes(writer.ToString())

Dim input As MemoryStream = New MemoryStream(bytes)

Dim output As MemoryStream = New MemoryStream()
''//Dim reader As TextReader = New StringReader(writer.ToString())

Dim doc As Document = New Document(PageSize.A4.Rotate, 30, 30, 30, 30)

Dim pdfWriter As PdfWriter = pdfWriter.GetInstance(doc, output)

doc.Open()

XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, doc, input, Nothing)

email.Attachments.Add(New Attachment(output, "Quote.pdf", "application/pdf"))

Dim smtp As New SmtpClient()
smtp.Credentials = New System.Net.NetworkCredential(System.Web.Configuration.WebConfigurationManager.AppSettings("SMTPUser").ToString(), System.Web.Configuration.WebConfigurationManager.AppSettings("SMTPPassword").ToString())
smtp.Send(email)
doc.Close()

我对这个过程很新,任何帮助都将不胜感激。另外,为了澄清我是否输出到文件而不是流,writer.toString()中的html按预期输出。

我很欣赏有关此事的任何想法。

谢谢,

编辑:

Dim page As Page = New Page()
Dim form As HtmlForm = New HtmlForm()
page.Controls.Add(form)

Dim bpa As UserControl_BpaReceipt = page.LoadControl("~/UserControl/BpaReceipt.ascx")
bpa._thisCart = _thisCart
form.Controls.Add(bpa)
Dim writer As StringWriter = New StringWriter()

Me.Server.Execute(page, writer, False)

email.IsBodyHtml = True


If (toValues.Value.Contains(";")) Then
    Dim toArray = toValues.Value.Split(";")
    For Each value In toArray
        email.To.Add(value)
    Next
Else
    email.To.Add(toValues.Value.ToString())
End If

email.Subject = txtSubject.Text
email.Body = "<br /><br /><br />" & writer.ToString()

email.From = New MailAddress("webadmin@managemobility.com")

Dim bytes = Encoding.UTF8.GetBytes(writer.ToString())

Dim input As MemoryStream = New MemoryStream(bytes)

Dim output As MemoryStream = New MemoryStream()
''//Dim reader As TextReader = New StringReader(writer.ToString())

Dim doc As Document = New Document(PageSize.A4.Rotate, 30, 30, 30, 30)

Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(doc, output)
pdfWrite.CloseStream = False
doc.Open()

XMLWorkerHelper.GetInstance().ParseXHtml(pdfWrite, doc, input, Nothing)
doc.Close()
email.Attachments.Add(New Attachment(output, "Quote.pdf", "application/pdf"))

Dim smtp As New SmtpClient()
smtp.Credentials = New System.Net.NetworkCredential(System.Web.Configuration.WebConfigurationManager.AppSettings("SMTPUser").ToString(), System.Web.Configuration.WebConfigurationManager.AppSettings("SMTPPassword").ToString())
smtp.Send(email)
''//doc.Close()

我编辑了代码以反映您的建议,我担心我仍然收到相同的输出空pdf 0kb。非常感谢您的建议,我对这个问题感到非常烦恼,并且非常感谢这些建议。

1 个答案:

答案 0 :(得分:0)

根据评论中的建议,请确保在开始尝试通过电子邮件发送之前将文件保存到磁盘。一旦你能做到这一点,然后继续附加它并发送电子邮件。下面是我使用System.Net.Mail类将pdf附加到电子邮件之前使用的代码片段。 (这是c#,但你明白了)

 output.Seek(0, SeekOrigin.Begin);
 System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
 contentType.MediaType = System.Net.Mime.MediaTypeNames.Application.Pdf;
 contentType.Name = "Quote.pdf";

 Attachment attachment = new Attachment(output, contentType);
 mail.Attachments.Add(attachment);