Itextsharp 似乎在填写完 pdf 表单后会损坏它;在适用于 WP7 的 Adobe Reader 中打开问题
我正在尝试从 asp.net C# 应用程序动态生成 pdf 表单。该表单已生成并通过电子邮件发送给某些用户。几个月前,人们就可以在 Adobe Reader 上打开它。
最近,有人要求用户能够在手机上阅读这些pdf附件。不幸的是,当这些人尝试在手机上打开 pdf 时,它显示“打开文档时出错。”
我尝试在我的 wp7 手机上打开原始表单,它可以工作。
我将其中一个不起作用的 pdf 的副本发送给免费的 pdf/验证器服务。该服务返回以下消息:
无法加载文件:不正确的“startxref”引用
这让我认为问题在于 Adobe Reader 正在重建文档中的某些元数据/索引。打开。由于大多数人拥有快速的机器,因此重建这些索引所花费的时间可以忽略不计。但据我了解,手机版本没有此功能,因此它正在读取 xstartref 并且失败。
以下是我的应用程序中似乎生成损坏文件的代码:
File.Copy(original, newpath);
FileStream fs = new FileStream(newpath, FileMode.Open);
PdfReader r = new PdfReader(fs);
PdfStamper stamper = new PdfStamper(r, fs);
AcroFields af = stamper.AcroFields;
af.SetField("Event", ef.eName); af.SetField("EventType", ef.EventType);
af.SetField("eStartDate", ef.eStartDate);
af.SetField("eStartTime",ef.eStartTime);
stamper.FormFlattening = true;
stamper.FreeTextFlattening = true;
stamper.Close();
r.Close();
fs.Close();
我做错了什么?我读过不正确关闭流、压模器和 pdfreader 会造成麻烦的地方。但我认为我正确地关闭了它。我在忽略什么?
I'm trying to dynamically generate a pdf form from an asp.net C# application. The form is generated and emailed to some users. People have been able to open it on Adobe Reader for months now.
Recently, there has been a request that users be able to read these pdf attachments on their mobile phones. Unfortunately, when these people try to open the pdfs on their phones, it says "There was an error opening the document."
I tried opening the original form on my wp7 phone and it works.
I sent a copy of one of the pdfs that is not working to the a free pdf/a validator service. The service came back with the following message:
Failed to load file: incorrect 'startxref' reference
This makes me think that the issue is that Adobe Reader is rebuilding some of the metadata/indices in the document when it opens. Because most people have fast machines, the time spent rebuilding these indices is negligible. But from what I understand the phone version does not have this capability, so it is reading the xstartref and failing.
Here is the code in my application that appears to be producing the corrupt file:
File.Copy(original, newpath);
FileStream fs = new FileStream(newpath, FileMode.Open);
PdfReader r = new PdfReader(fs);
PdfStamper stamper = new PdfStamper(r, fs);
AcroFields af = stamper.AcroFields;
af.SetField("Event", ef.eName); af.SetField("EventType", ef.EventType);
af.SetField("eStartDate", ef.eStartDate);
af.SetField("eStartTime",ef.eStartTime);
stamper.FormFlattening = true;
stamper.FreeTextFlattening = true;
stamper.Close();
r.Close();
fs.Close();
What am I doing wrong? I've read where not closing the stream, stamper and pdfreader properly can create trouble. But I think I am closing it properly. What am I overlooking?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将
PdfReader
绑定到源文档,并将PdfStamper
绑定到目标文档。否则,您会在写入文档的同时读取文档,这不是一个好主意。
You should be binding the
PdfReader
to your source document and yourPdfStamper
to your destination document.Otherwise you're reading from a document while you write to it which isn't a good idea.