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();