填写后,Itextsharp似乎正在破坏pdf格式;在Adobe Reader for WP7中打开问题 [英] Itextsharp seems to be corrupting a pdf form after filling it out; problems opening in Adobe Reader for WP7

查看:157
本文介绍了填写后,Itextsharp似乎正在破坏pdf格式;在Adobe Reader for WP7中打开问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从asp.net C#应用程序动态生成pdf表单。生成表单并通过电子邮件发送给某些用户。人们已经能够在Adobe Reader上打开它几个月了。

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.

最近,有人要求用户能够在他们的手机上阅读这些pdf附件。不幸的是,当这些人试图在他们的手机上打开pdf时,它会说打开文档时出错。

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."

我试过在我的wp7手机上打开原始表格并且它可以正常工作。

I tried opening the original form on my wp7 phone and it works.

我发送了一份不适用于免费pdf /验证器服务的pdf副本。该服务返回时显示以下消息:

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:

无法加载文件:错误的'startxref'参考

这让我觉得问题是Adobe Reader在文档打开时正在重建文档中的一些元数据/索引。由于大多数人拥有快速机器,因此重建这些指数所花费的时间可以忽略不计。但根据我的理解,手机版本没有此功能,因此它正在读取xstartref并失败。

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

我做错了什么?我已经读过没有正确关闭流,压模和pdfreader的地方会造成麻烦。但我认为我正在关闭它。我忽略了什么?

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?

推荐答案

你应该绑定 PdfReader 到您的源文档和 PdfStamper 到您的目标文档。

You should be binding the PdfReader to your source document and your PdfStamper to your destination document.

//Get rid of the below line completely
//File.Copy(original, newpath);
FileStream fs = new FileStream(newpath, FileMode.Open);
PdfReader r = new PdfReader(original);
PdfStamper stamper = new PdfStamper(r, fs);

否则你在写文件时正在阅读文件,这不是一个好主意。

Otherwise you're reading from a document while you write to it which isn't a good idea.

这篇关于填写后,Itextsharp似乎正在破坏pdf格式;在Adobe Reader for WP7中打开问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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