简体   繁体   中英

iTextSharp - How to convert Document to byte[]

I need to attach a pdf I created in memory to an email. Attachments can take a stream. So I believe I need to convert a iTextSharp Document object to stream. How can I do that? I tried serializing the Document object to a stream but it is not "marked as serializable".

Here is a approach, whith a new sample A4 document with "hello world" in it.

using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
    //creating a sample Document
    iTextSharp.text.Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 30f, 30f, 30f, 30f);
    iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, ms);
    doc.Open();
    doc.Add(new iTextSharp.text.Chunk("hello world"));
    doc.Close();
    byte[] result = ms.ToArray();
}

Look at iText.pdf.PdfWriter . There are methods that take a stream.

Here's a sample for streaming in ASP.NET- link text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM