iTextSharp 生成的 PDF 现在会导致 Adob​​e Reader X 中的“保存"对话框 [英] iTextSharp-generated PDFs now cause Save dialog in Adobe Reader X

查看:25
本文介绍了iTextSharp 生成的 PDF 现在会导致 Adob​​e Reader X 中的“保存"对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 iTextSharp 生成 PDF 文档已经一年多了.不幸的是,随着 Adob​​e Reader X 的发布,我的 PDF 现在会导致您想保存吗?"关闭 PDF 文档时出现的对话框.不是用 iTextSharp 生成的 PDF 不会发生这种情况.对于整天打开和关闭PDF文档的用户来说,这真的很烦人.我可以设置 iTextSharp 中的任何属性来防止这种情况发生吗?

I have been using iTextSharp to generate PDF documents for over a year. Unfortunately, with the release of Adobe Reader X, my PDFs now cause a "Do you want to Save?" dialog to appear when closing the PDF document. This does not happen with PDFs that are not generated with iTextSharp. It's really annoying for my users who are opening and closing PDF documents all day long. Are there any properties in iTextSharp that I can set to prevent this from happening?

如果有帮助,我正在使用 PdfReader 从现有 PDF 文档中读取数据(此原始文档不会导致出现保存"对话框).然后我使用 PdfWriter 创建一个新文档,并使用 AddTemplate 将原始文档的一部分复制到新文档中.

If it helps, I am using a PdfReader to read data from an existing PDF document (this original document does not cause the Save dialog to appear). I then use a PdfWriter to create a new document and AddTemplate to copy a portion of the original document to the new one.

推荐答案

问题是这一行:

Response.OutputStream.Write(MS.GetBuffer(), 0, MS.GetBuffer().Length)

GetBuffer 方法返回大于实际内容的整个内部缓冲区.坏的 PDF 末尾有大约 10kb 的垃圾内容(零字节),好的 PDF 只有几个垃圾字节.使用内存流的ToArray()方法获取PDF文件,问题就解决了.您还将获得更小的文件.

The GetBuffer method returns the entire internal buffer which is larger that the actual content. The bad PDF has about 10kb of garbage content at the end (bytes of zero), the good PDF has only a few garbage bytes. Use the ToArray() method of the memory stream to get the PDF file and the problem will be fixed. You will also get smaller files.

byte[] pdf = MS.ToArray();
Response.OutputStream.Write(pdf, 0, pdf.Length);

同时用pdf数组的长度设置Content-Length".

Also set the "Content-Length" with the length of the pdf array.

这篇关于iTextSharp 生成的 PDF 现在会导致 Adob​​e Reader X 中的“保存"对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆