2

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();
16
  • Where are you using your memory stream and saving the actual file? Commented Sep 22, 2016 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. Commented Sep 22, 2016 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(); Commented Sep 22, 2016 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. Commented Sep 22, 2016 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. Commented Sep 22, 2016 at 20:26

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.