我一直在尝试使用iTextSharp创建PDF并遇到问题。在关闭pdf后,Acrobat Reader会提示用户“你想保存更改...”iTextSharp - 关闭时出现“您要保存”提示吗pdf
这似乎是一个常见问题,并且可能有十几个关于堆栈溢出的问题,不同的方案。我尝试了尽可能多的解决方案,但无济于事。
我的代码如下。我用一个段落创建一个简单的pdf,使用MemoryStream和PdfWriter。然后我将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 - 但我的程序不使用压模。 Bruno在该链接上提供了一个答案,并提到了acroform字典,但我不确定如何从该字典中删除条目,并且提问的用户无法解决他们的问题。
我需要使用PdfWriter。我也研究过使用文件流而不是像这里提到的输出流 - iTextSharp-generated PDFs cause save dialog when closing - 但我需要将pdf下载到客户端,而不是将其保存在磁盘上。
你有没有尝试将数组写入文件除了服务它?然后检查服务的版本是否像保存的版本一样? – mkl
明天我肯定会试一试。你能详细说明你的“服务”是什么意思吗?对不起,我不知所措。 –
*服务*从您的网络服务返回,即你已经尝试。 – mkl