0
votes

I have this simple piece of code which I took from another question here with the same topic, corrupted PDF files.

I've tried to implement the described solution, along with other references, but have met no sucess yet.

Here are the two functions generating my example PDF:

private void ShowPdf (byte[] str)
{
    var Response = HttpContext.Current.Response;

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

    Response.BinaryWrite(str);
    Response.End();
    Response.Flush();
    Response.Clear();
}
private byte[] CreatePDF2()
{
    Document doc = new Document(PageSize.LETTER, 50, 50, 50, 50);

    using (MemoryStream output = new MemoryStream())
    {
        PdfWriter wri = PdfWriter.GetInstance(doc, output);
        doc.Open();

        Paragraph header = new Paragraph("Test bug") { Alignment = Element.ALIGN_CENTER };
        Paragraph paragraph = new Paragraph("test.");
        Phrase phrase = new Phrase("testnewline. \nnewline hapenned.");
        Chunk chunk = new Chunk("Chucnk cauncuanocnaacoocsinasiocniocsanacsoi chunk.");

        doc.Add(header);
        doc.Add(paragraph);
        doc.Add(phrase);
        doc.Add(chunk);

        doc.Close();
        return output.ToArray();
    }
}

Then at an request, I just consume it as in:

ShowPdf(CreatePDF2());

The problem is that this generates a file called "Response.pdf.pdf", which is corrupt and can't be opened.

How can I solve this problem?

Obs.: I am currently using iTextSharp 4.1.6

1
I ran both your iText code as well as your ASP.Net code and both worked as expected so I'm pretty sure your problem is elsewhere. Try opening your PDF in notepad and look text content before the %PDF-1.4 at the beginning of the file or after the %%EOF at the end of the file. - Chris Haas
Hey @ChrisHaas first of all, thanks for the help. I could not find any of these texts on the PDF that was generated. - Malavos
@ChrisHaas do you think this sort of problem could be related to the version I am currently using? I am using 4.1.6 for the LGPL license. Could there be a know bug or some changes that I will have to adapt to? - Malavos
@Malavos This post explains why your assumption that you can use 4.1.6 (corresponding with 2.1.6 in Java) is wrong: Can iText 2.1.7 or earlier be used commercially? Make sure you inform your customer of the technical and legal implications of your choice for iTextSharp 4.1.6. - Bruno Lowagie
@Malavos Quality has its price. We have improved iText dramatically, but to do so, we need to hire people. We can't hire people if nobody is paying for their use of iText. See How can large open source projects be monetized? Open source is meant for ethical use of software, not for abuse in the sense of: "we take something and we never give anything back" (which is the perception you create with your comments). - Bruno Lowagie

1 Answers

1
votes

Try this for output variable,

FileStream output = new FileStream(Server.MapPath("MyFirstPDF.pdf"), FileMode.Create);