iTextSharp generating pdfs, compiles and runs without error but no PDF appears

拟墨画扇 提交于 2019-12-11 20:01:38

问题


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().Length);
Response.OutputStream.Flush();            
Response.OutputStream.Close();
ms.Close();

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/6451672/itextsharp-generating-pdfs-compiles-and-runs-without-error-but-no-pdf-appears

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!