2
votes

I've been attempting to create a pdf using iTextSharp and have run into an issue. Upon closing the pdf, Acrobat Reader prompts the user "Do you want to save changes..."

This seems to be a common issue, and there are probably a dozen questions on stack overflow about it, and just as many different solutions. I've tried as many solutions I can find, to no avail.

My code is below. I create a simple pdf with one paragraph, using MemoryStream and the PdfWriter. I then return the MemoryStream as an array, and I then use the response.outputstream to download the file to the client.

protected void lnkbtnDownloadPdf_Click(object sender, EventArgs e)
{
        var Pdf = DownloadPdf();
        Response.ContentType = "application/pdf;";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + "test.pdf");
        Response.OutputStream.Write(Pdf, 0, Pdf.Length);
        Response.OutputStream.Close();
}


public static byte[] DownloadPdf()
{
    using (MemoryStream ms = new MemoryStream())
    {
        Document doc = new Document(PageSize.LETTER.Rotate());
        PdfWriter writer = PdfWriter.GetInstance(doc, ms);
        doc.Open();
        doc.Add(new Paragraph("testtesttesttesttesttestesttest"));
        doc.Close();
        writer.Close();
        return ms.ToArray();
    }
}

I've tried this - iTextSharp-generated PDFs now cause Save dialog in Adobe Reader X - and I still get the save dialog.

I've also tried to implement this - Using iTextSharp to write data to PDF works great, but Acrobat Reader asks 'Do you want to save changes' when closing file - but my program doesn't use the stamper. Bruno has an answer on that link as well mentioning the acroform dictionary, but I'm not sure how to remove entries from that dictionary, and the user who asked the question was unable to fix their issue doing that anyways.

I need to use the PdfWriter. I've also looked into using the filestream instead of the outputstream like mentioned here - iTextSharp-generated PDFs cause save dialog when closing - but I need to download the pdf to the client and not save it on disk.

1
Have you tried writing the array into a file in addition to serving it? And then checking whether the served version behaves like the saved one?mkl
I can definitely give that a try tomorrow. Could you elaborate on what you mean by "serving it"? Sorry, I'm at a loss.Austin Donley
Serving as in returning from your web service, i.e. what you already try.mkl
Also, are you clearing the HTTP buffer before serving?Chris Haas
One way or another, Adobe Acrobat/Reader is "fixing" your PDF for you and when it does it always asks you to save. Most often the culprit is extra bits before or after the PDF's binary streamChris Haas

1 Answers

1
votes

After some time I figured out it wasn't an Itext problem at all (to my knowledge).

I added -

Response.End();

-at the end of the lnkbtnDownloadPdf_Click function and it worked. Acrobat no longer asks the user to save, when they close my PDFs.