0

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

Community
  • 1
  • 1
Malavos
  • 429
  • 3
  • 12
  • 27
  • 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 Jun 22 '16 at 18:08
  • Hey @ChrisHaas first of all, thanks for the help. I could not find any of these texts on the PDF that was generated. – Malavos Jun 22 '16 at 18:13
  • @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 Jun 22 '16 at 18:22
  • Why don't you try with 5.5.9? – Amedee Van Gasse Jun 22 '16 at 19:37
  • @AmedeeVanGasse 4.1.6 is LGPL. That means it can be used in closed-source projects, which is the one I am currently working this on. 4.1.6 is the last release before iTextSharp moved to AGPL. – Malavos Jun 22 '16 at 19:39
  • I know, but for a MCVE that only reproduces your issue, it shouldn't be an issue to use iTextSharp AGPL and get an answer to your question 3 comments above? I'm not saying that you should plug 5.5.9 into your full product codebase, just extract a small piece of it that reproduces your problem and try there with 5.5.9. In fact, by posting your code in your question, you have already made that small part open source. – Amedee Van Gasse Jun 22 '16 at 19:47
  • @AmedeeVanGasse that's a bit off, mostly because this code is just a basic function with no data being added to the pdf file, and all of SO code posted becomes CC, so... But now, if you mean about the text content, I've just tried with AGPL, my problem is just the same. If you think of anything else I can try, please, do say! – Malavos Jun 22 '16 at 19:53
  • Can you post the PDF that's being generated? – Chris Haas Jun 22 '16 at 22:39
  • 2
    @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?](http://developers.itextpdf.com/question/can-itext-217-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 Jun 23 '16 at 08:13
  • @BrunoLowagie thank you for the link. Although this does not solves my problem, this have changed our mind on using iText. We will move to other library, after reading it. Thanks! – Malavos Jun 23 '16 at 12:31
  • 1
    @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?](http://opensource.stackexchange.com/questions/88/how-can-large-open-source-projects-be-monetized/210#210) 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 Jun 23 '16 at 12:42
  • @BrunoLowagie I understand that completely and I do agree with you, I worked in another project where we fully used your library and was satisfied with it. But I live in Brazil, and this project now is albeit small, and the dollar price does not bode well for the license. The pdf manipulation is quite simple, so it does not justify the usage of such license. It's not a problem with the product per se, if that's what you got from my comment, please, do not take it that way. Your product is awesome. But the dollar price here and the small scope of the project at this time would not justify it. – Malavos Jun 23 '16 at 12:47
  • @Malavos OK, I understand. Thank you for clarifying. – Bruno Lowagie Jun 23 '16 at 12:50
  • @BrunoLowagie no problemo. We like your work and product here in Brazil very much; Don't take it in the wrong way. Keep making it a great product. – Malavos Jun 23 '16 at 12:54

1 Answers1

1

Try this for output variable,

FileStream output = new FileStream(Server.MapPath("MyFirstPDF.pdf"), FileMode.Create);
SilentCoder
  • 1,970
  • 1
  • 16
  • 21
  • I'm going to try this and I'll comment here what hapenned. – Malavos Jun 22 '16 at 17:51
  • this solution did not work out for me, although I am not using itextsharp anymore, this could help future users. Thanks. – Malavos Jun 23 '16 at 22:27
  • 1
    Update: I replicated the problem in another machine to update my question, and this solution worked in the case. Have no idea why it did not work on the old machine, thou. – Malavos Jan 26 '18 at 15:21