Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iTextSharp - How to convert Document to byte[]

Tags:

c#

stream

pdf

itext

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".

like image 632
Gus Cavalcanti Avatar asked Jul 28 '09 07:07

Gus Cavalcanti


People also ask

What is the use of iTextSharp DLL?

What is ITextSharp? iTextSharp is a free and open source assembly that helps to convert page output or HTML content in a PDF file. Now add that DLL in the application.

What is phrase in iTextSharp?

A Phrase is a series of Chunk s. A Phrase has a main Font , but some chunks within the phrase can have a Font that differs from the main Font . All the Chunk s in a Phrase have the same leading .


2 Answers

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();
}
like image 186
fubo Avatar answered Oct 21 '22 10:10

fubo


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

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

like image 20
RichardOD Avatar answered Oct 21 '22 10:10

RichardOD