1

I've searched through most of the 300+ posts on this topic and cannot find one that addresses this issue specifically.

I tried the simplest of file creators with the same result: (from http://www.codeproject.com/Articles/686994/Create-Read-Advance-PDF-Report-using-iTextSharp-in#1) I found this link in another post.

public byte[] generatePublicationCitationReport(List<int> pubIDs)
{
//Step 1: Create a System.IO.FileStream object:
    MemoryStream ms = new MemoryStream();

//Step 2: Create a iTextSharp.text.Document object:
    Document doc = new Document();

//Step 3: Create a iTextSharp.text.pdf.PdfWriter object. It helps to write the Document to the Specified FileStream:
    PdfWriter writer = PdfWriter.GetInstance(doc, ms);

//Step 4: Openning the Document:
    doc.Open();

//Step 5: Adding a Paragraph by creating a iTextSharp.text.Paragraph object:
    doc.Add(new Paragraph("Hello World"));

//Step 6: Closing the Document:
    doc.Close();

    return ms.ToArray();
}

The code was modified slightly changing the "filestream" to "memorystream" and passing that back to the calling function to open the file.

The code above generates a 0 byte file and tries to open it. When opening fails, I get an error message indicating "Failed to load PDF file."

I'm trying to generate a PDF file from a list of citations created from data in an SQL database. I'm getting the data properly and can display it using Response.Write.

In my code I add a loop to create each citation individually and add it to the paragraph.

iTextSharp.text.Paragraph paragraph1 = new iTextSharp.text.Paragraph();
iTextSharp.text.Paragraph paraCitations = new iTextSharp.text.Paragraph();
iTextSharp.text.Paragraph paragraph3 = new iTextSharp.text.Paragraph();
iTextSharp.text.Chunk chunk1 = new iTextSharp.text.Chunk("Chunky stuff here...");
paragraph1.Add("Paragraph stuff goes here...");

for (int i = 0; i < pubIDs.Count; i++)
{
    string pubCitation = createPubCitation(pubIDs[i]);
    chunk1.Append(pubCitation);
    paraCitations.Add(chunk1);
}
paragraph3.Add("New paragraph - paraCitations - goes here");

doc.Add(paragraph1);
doc.Add(paraCitations);
doc.Add(paragraph3);
doc.Close();

return ms.toArray();

}

Any suggestions? Pointers? Answer?

Thanks, Bob

This is the call to and return from the procedure to create the PDF file and open it...

pubCitationAsPDF = p.generatePublicationCitationReport(pubIDs);

Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=publicationCitations.pdf");

Response.End();
Response.Flush();
Response.Clear();
R Loomas
  • 303
  • 1
  • 2
  • 11
  • Where are you using your memory stream and saving the actual file? – Darren Wainwright Sep 22 '16 at 20:11
  • Thousands of developers have succeeded in using iTextSharp before you, using code that looks exactly like yours. If you get 0 bytes, something went wrong even before the `doc.Open();` because that line causes the first bytes to be written (more specifically: the header of the PDF file). There is an obvious error in your snippet though: you use the `AddTitle()` method after the document was opened. That throws an exception. – Bruno Lowagie Sep 22 '16 at 20:11
  • @Darren, this is the code that calls the procedure and opens the file. p.generatePublicationCitationReport(pubIDs); Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", "attachment; filename=publicationCitations.pdf"); Response.End(); Response.Flush(); Response.Clear(); – R Loomas Sep 22 '16 at 20:19
  • @BrunoLowagie The "AddTitle()" method was one of several attempts to try something to get it to work. That is no longer in the code and it still does not work. – R Loomas Sep 22 '16 at 20:24
  • @RLoomas Well, you shouldn't expect a good answer if the question doesn't reflect the actual situation. As I said: thousands of developers have succeeded where you fail. It looks as if you're trying to run before you can walk. Have you tried the simple examples first? They work perfectly, don't they? If not, tell us what goes wrong. – Bruno Lowagie Sep 22 '16 at 20:26
  • @BrunoLowagie: In the original post, I show the code from the simplest of examples and it produces the same 0-byte file. I understand "thousands of developers have succeeded where I fail." That's why I'm here for help. – R Loomas Sep 22 '16 at 20:47
  • In the code you show, you create a `MemoryStream`. We don't see how you turn this stream (that could very well contain a valid PDF) into a file (the one that is 0 bytes). Maybe you are blaming iTextSharp for something that is wrong in your code. Please understand that it is very hard to believe your allegation, knowing that so many developers use iTextSharp without experiencing any problem. The Hello World example just works. – Bruno Lowagie Sep 22 '16 at 20:52
  • @BrunoLowagie: I can see you believe everyone not as smart as you is a troll. Some people have different experiences. When I tell you the simple "Hello world" example doesn't work, it doesn't work. And, I'm not the only one on my dev team that has seen it "not work." I'm editing the original post to include the call and return. – R Loomas Sep 22 '16 at 21:05
  • I'm not smart. I look at the numbers. You've found 300+ posts from people who have a working iTextSharp example. I have reached 1.9M developers on Stack Overflow by providing answers, accumulating a reputation of 43K+. That wouldn't be possible if *it didn't work.* – Bruno Lowagie Sep 22 '16 at 21:08
  • I'm tempted to close the question, using this question as duplicate: http://stackoverflow.com/questions/20031982/itextsharp-create-new-document-as-byte – Bruno Lowagie Sep 22 '16 at 21:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123986/discussion-between-bruno-lowagie-and-r-loomas). – Bruno Lowagie Sep 22 '16 at 21:11
  • @BrunoLowagie, This is not a duplicate question, not all of the previous questions address the problem. Many are full of answers suggesting different tools (that I don't have access to). – R Loomas Sep 22 '16 at 21:13
  • As I said in the chat: Your code is extremely sloppy. For instance: you create a variable `pubCitationAsPDF` but you aren't using it anywhere. You're not sending a single byte through the `Response`. If that is really your code, you shouldn't be surprised that no bytes are sent to the browser. – Bruno Lowagie Sep 22 '16 at 21:15
  • Your question might as well be a duplicate of http://stackoverflow.com/questions/3623797/how-to-convert-pdf-byte-array-to-downloadable-file-using-itextsharp – Bruno Lowagie Sep 22 '16 at 21:17
  • Hmmm... I see that you work for SPAWAR. I have been helping out people at SPAWAR in San Diego many years ago. You probably have colleagues with a decade of experience using iText. – Bruno Lowagie Sep 22 '16 at 21:19
  • @R Loomas. You're not adding your pdf stream to your response hence the 0byte file. Add the stream to the response content stream. It's not an itext sharp issue. Perhaps also don't bother replying to Bruno, seems he/she can't remember a time when they didn't know all :-). – Darren Wainwright Sep 23 '16 at 11:13

1 Answers1

1

As per comments, it appears your issue is more related to how you download the file, vs creating the file.

Your code for downloading does not include adding the bytes from your memory stream to your response.

Change your download code to this:

Response.Clear();
Response.ContentType = "application/force-download";
Response.AddHeader("content-disposition", "attachment; filename=publicationCitations.pdf");
// This is the piece you're missing
Response.BinaryWrite(p.generatePublicationCitationReport(pubIDs));   
Response.End();
Darren Wainwright
  • 30,247
  • 21
  • 76
  • 127