iTextSharp - "Do you want to save" 关闭 pdf 时提示

iTextSharp - "Do you want to save" prompt when closing pdf

我一直在尝试使用 iTextSharp 创建 pdf,但 运行 遇到了问题。关闭 pdf 后,Acrobat Reader 会提示用户 "Do you want to save changes..."

这似乎是一个常见问题,关于堆栈溢出的问题可能有十几个,而且解决方案也很多。我已经尝试了尽可能多的解决方案,但无济于事。

我的代码如下。我使用 MemoryStream 和 PdfWriter 创建了一个只有一段的简单 pdf。然后我 return 将 MemoryStream 作为数组,然后我使用 response.outputstream 将文件下载到客户端。

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

我试过了 - iTextSharp-generated PDFs now cause Save dialog in Adobe Reader X - 我仍然看到保存对话框。

我也尝试实现这个 - Using iTextSharp to write data to PDF works great, but Acrobat Reader asks 'Do you want to save changes' when closing file - 但我的程序不使用压模。 B运行o 对 link 有一个答案,也提到了 acroform 字典,但我不确定如何从该字典中删除条目,并且提出问题的用户无法修复他们的问题无论如何都要这样做。

我需要使用 PdfWriter。我也研究过使用文件流而不是此处提到的输出流 - iTextSharp-generated PDFs cause save dialog when closing - 但我需要将 pdf 下载到客户端而不是将其保存在磁盘上。

一段时间后,我发现这根本不是 Itext 问题(据我所知)。

我加了-

Response.End();

-在 lnkbtnDownloadPdf_Click 函数的末尾,它起作用了。当用户关闭我的 PDF 时,Acrobat 不再要求用户保存。