Reputation: 11
itext sharp is creating a corrupt PDF when sending on email with c# asp.net but working on localhost when downloading on local server.
using (MemoryStream ms = new MemoryStream())
{
try
{
Document document = new Document(PageSize.A4, 25, 25, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(document, ms);
writer.CloseStream = false;
document.Open();
Font fnt = FontFactory.GetFont("Times New Roman", 12);
PdfPTable PdfTable = new PdfPTable(1);
var path = Server.MapPath(@"~/image.png");
iTextSharp.text.Image logopng =
iTextSharp.text.Image.GetInstance(path);
document.Add(logopng);
logopng.ScaleAbsolute(120f, 155.25f);
logopng.SpacingBefore = 10f;
logopng.SpacingAfter = 1f;
logopng.Alignment = Element.ALIGN_LEFT;
string dtt = todaydate.Text;
PdfPCell dtt1 = new PdfPCell(new Phrase(new Chunk(dat + dtt)));
PdfTable.AddCell(dtt1);
string buy;
buy = "Hello World";
string frst = first_name.Text;
PdfPCell frst1 = new PdfPCell(new Phrase(new Chunk(buy + frst)));
PdfTable.AddCell(frst1);
string city = city_add.Text;
PdfPCell city1 = new PdfPCell(new Phrase(new Chunk(city)));
PdfTable.AddCell(city1);
document.Add(PdfTable);
document.Close();
writer.Close();
sendmail(ms);
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=payment agreement.pdf");
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
catch
{
ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Mail not sent');", true);
}
}
}
private void sendmail(Stream ms)
{
string FromMail = "[email protected]";
string ToMail = email_id.Text;
string Subject = "Payment Agreement";
string Body = "Dear Customer ";
using (Attachment att = new Attachment(ms, "payment agreement.pdf", MediaTypeNames.Application.Pdf))
{
using (MailMessage mm = new MailMessage(
FromMail, ToMail, Subject, Body))
{
mm.Attachments.Add(att);
SmtpClient smtp = new SmtpClient();
smtp.Host = "server name";
smtp.Port = 25;
smtp.Send(mm);
}
}
}
Upvotes: 1
Views: 2478
Reputation: 77528
Remove the following line:
sendmail(ms);
Use this line instead:
sendmail(new MemoryStream(ms.ToArray()));
Additional note: I see that you use ms.GetBuffer()
in your code. Please read the following remark on Microsoft's Developer Network:
Note that the buffer contains allocated bytes which might be unused. For example, if the string "test" is written into the MemoryStream object, the length of the buffer returned from
GetBuffer
is 256, not 4, with 252 bytes unused. To obtain only the data in the buffer, use theToArray
method; however,ToArray
creates a copy of the data in memory.
When you use GetBuffer
on a PDF, you risk a PDF that has extra bytes at the end. This can be problematic, because a PDF viewer starts reading a PDF at the end of the file (that's where the cross-reference table can be found). If a PDF doesn't end with %%EOF
, but with some random extra bytes to get a multiple of 256, your PDF viewer might assume that your PDF is corrupt.
Upvotes: 4
Reputation: 46929
I think you need to move the position of the stream to the beginning. Right now it is at the end and an "empty" file would be attached. So before you send it, set the position to 0.
ms.Position = 0;
sendmail(ms);
Upvotes: 3