Reputation: 1181
My code
using (iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4.Rotate()))
{
MemoryStream memoryStream = new MemoryStream();
PdfWriter.GetInstance(document, memoryStream);
document.Open();
List<IElement> objects = HTMLWorker.ParseToList(new StringReader(html), null);
foreach (IElement element in objects)
{
if (element.Chunks.Select(x => x.Content).Contains("page_break_here")) document.Add(Chunk.NEXTPAGE);
else document.Add(element);
}
byte[] docArray = memoryStream.ToArray();
}
memoryStream.Length is 16
and when I try this:
PdfWriter.GetInstance(document, new FileStream(fullPdfFileAddress, FileMode.Create));
it creates file just fine, but i need to write it into memoryStream.
Upvotes: 2
Views: 3922
Reputation: 54887
Building on my comment: The document will only be fully written to the stream when it is closed. However, PdfWriter
closes the associated stream as well by default, giving rise to your ObjectDisposedException
. You can prevent this from happening by setting its CloseStream
property to false
:
using (var memoryStream = new MemoryStream())
{
using (var document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4.Rotate()))
{
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
writer.CloseStream = false;
// Write PDF here.
}
byte[] docArray = memoryStream.ToArray();
}
Upvotes: 4