I've tried a number of different variations to output a very basic PDF from memory but all seem to return the same result, which is to say it doesn't actually return anything. The code compiles and runs without error but when VS finishes processing the code nothing happens.
I'm using VS2008 and iTextSharp v5.1.1
Does anyone have any suggestions please?
Here is my code in its current state:
MemoryStream ms = new MemoryStream();
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, ms);
writer.CloseStream = false;
doc.Open();
doc.Add(new Paragraph("Test Content"));
doc.Add(new Paragraph(DateTime.Now.ToString()));
doc.Close();
Response.ContentType = "application/pdf";
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Len开发者_如何学JAVAgth);
Response.OutputStream.Flush();
Response.OutputStream.Close();
ms.Close();
One thing that I learned early on, don't use GetBuffer()
, use ToArray()
. See this post:
iTextSharp-generated PDFs now cause Save dialog in Adobe Reader X
I found what was causing my issue, the code was in a button_click event where the button control was inside an ajax update panel, as soon as i moved the button outside of the update panel it worked perfectly. Not sure if this is a fundamental mistake on my part or a bug with update panels so I'm off to have a read about them.
@Mark Storer, I appreciate now that this wasn't an ITextSharp problem, however when I wrote this I believed it to be, apologies to all for the mistake.
精彩评论