1

I'm developing an ASP.NET application where I have to send an PDF based on a Table created dinamically on the page as attachment on a email. So I have a function that creates the PDF as iTextSharp Document and returns it. If i try just to save this document, it works fine but I'm having a bad time trying to make it as Stream. I tried several things already, but I always get stuck at some point.

I tried to serialize it, but appears that Document is not serializable. Then I tried to work with PdfCopy, but I couldn't find out how to use this to my problem in specific.

The code right now is like this:

//Table,string,string,Stream
//This document returns fine
Document document = Utils.GeneratePDF(table, lastBook, lastDate, Response.OutputStream);

using (MemoryStream ms = new MemoryStream())
{
    PdfCopy copy = new PdfCopy(document, ms);
    //Need something here to copy from one to another! OR to make document as Stream      
    ms.Position = 0;
    //Email, Subject, Stream
    Utils.SendMail(email, lastBook + " - " + lastDate, ms);
}
Markissimo
  • 309
  • 2
  • 9
  • 23
  • Depending on your iTextSharp version, streams are closed automatically after the PDFs are completely created. `PdfWriter` and derived classes (e.g. `PdfCopy`) have an attribute to stop this. You might need to toggle this attribute. – mkl Apr 02 '13 at 22:45

2 Answers2

3

Try to avoid passing the native iTextSharp objects around. Either pass streams, files or bytes. I don't have an IDE in front of me right now but you should be able to do something like this:

byte[] Bytes;
using(MemoryStream ms = new MemoryStream()){
    Utils.GeneratePDF(table, lastBook, lastDate, ms);
    Bytes = ms.ToArray();
}

Then you can either change your Utils.SendMail() to accept a byte array or just wrap it in another stream.

EDIT

You might also be able to just do something like this in your code:

using(MemoryStream ms = new MemoryStream()){
    Utils.GeneratePDF(table, lastBook, lastDate, ms);
    ms.Position = 0;
    Utils.SendMail(email, lastBook + " - " + lastDate, ms);
}
Community
  • 1
  • 1
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • Thanks @ChrisHaas, worked like a charm! I tried something like this on my first tests, but I can't be sure why this didn't worked back there, must have forget something. Thanks again! – Markissimo Apr 03 '13 at 11:38
2

I did this in the past by doing something like the following:

        using (Document doc = new Document())
        {
            MemoryStream msPDFData = new MemoryStream();
            PdfWriter writer = PdfWriter.GetInstance(doc, msPDFData);
            doc.Open();
            doc.Add(new Paragraph("I'm a pdf!");
        }

If you need access to the raw data you can also do

byte[] pdfData = msPDFData.ToArray();
Kendall Trego
  • 1,975
  • 13
  • 21
  • Make sure to use `ToArray()` instead of `GetBuffer()` http://stackoverflow.com/a/6454435/231316 – Chris Haas Apr 02 '13 at 21:25
  • Thanks for the answer! But i do this when I'm creating the PDF, I wanted something that I could reuse the document with the method that sends the email. @ChrisHaas answer worked very nicely! – Markissimo Apr 03 '13 at 11:36