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.