Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

iTextSharp-generated PDFs now cause Save dialog in Adobe Reader X

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?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is this line:

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...