2

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);
}
1
  • 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. Commented Apr 2, 2013 at 22:45

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

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!
3

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();

2 Comments

Make sure to use ToArray() instead of GetBuffer() stackoverflow.com/a/6454435/231316
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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.