我一直试图使用iTextSharp创建一个pdf并遇到了一个问题 . 关闭pdf后,Acrobat Reader会提示用户“是否要保存更改...”
这似乎是一个常见的问题,关于它的堆栈溢出可能有十几个问题,并且有许多不同的解决方案 . 我尝试了尽可能多的解决方案,但无济于事 .
我的代码如下 . 我使用MemoryStream和PdfWriter创建一个带有一个段落的简单pdf . 然后我将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 - 但我的程序没有't use the stamper. Bruno has an answer on that link as well mentioning the acroform dictionary, but I'我不知道如何从该字典中删除条目,并且提出问题的用户无法解决他们的问题 .
我需要使用PdfWriter . 我也研究过使用文件流而不是像这里提到的输出流 - iTextSharp-generated PDFs cause save dialog when closing - 但我需要将pdf下载到客户端而不是将其保存在磁盘上 .
1 回答
过了一段时间,我发现它根本不是一个Itext问题(据我所知) .
我补充说 -