0

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();
9
  • There's a good working version here: bit.ly/kEWMWn. Might be worth your while to compare your attempt with it to see if you're missing anything. I can't remember if iTextSharp writes to an error log - might be worth checking that too if so. Commented Jun 23, 2011 at 9:08
  • Hi, thanks very much for your suggestion. There were some differences, unfortunately none of which made any difference to the result I'm receiving. Could the issue perhaps have anything to do with the browser I'm using? I'm currently running IE9, but am unable to test in other browsers as the website has been written to be browser specific to IE only. Commented Jun 23, 2011 at 9:25
  • I think it might be worth testing with other browsers if you can, but I would have thought its more likely to be a serverside issue. Does your code throw any exceptions? Can you step through it in the debugger? And is there an error log you can look at? Commented Jun 23, 2011 at 9:44
  • Ok, the code does not throw any exceptions and I have stepped through it multiple times (everytime I change something infact), which is why it's quite confusing. There don't appear to be any logs, none of which I can see anyway. I am using third party logging but as VS isn't throwing any errors those logs are clear. Commented Jun 23, 2011 at 9:49
  • Edit: I have managed to test in Chrome, which results in the exact same issue. Commented Jun 23, 2011 at 9:55

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your reply, I've just tried this and didn't make any difference. I'm sure this is IIS related and not code related, as all variations of code seem to end in the same result. I've already checked mimetypes and allowed filenameextensions but there muast be something else.
There's been a development... I've just created a brand new website on my local machine to test the code, which works! But for some reason it just will not work in my actual project.
Then this isn't an iText problem, it's a Response problem. I suggest you retag appropriately.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.