根据我之前的帖子ITextsharp to edit existing pdf,我能够以交互方式保存PDF。之后我开始编写以下代码来修改内容并将其保存为PDF,如下所示,但当我尝试打开文件时,它会说There was an error opening in the document. The file is damaged and could not be repaired
这是我的代码
PdfReader reader = new PdfReader(@"E:\\Test.pdf");
using (MemoryStream memoryStream = new MemoryStream())
{
PdfStamper pdfStamper = new PdfStamper(reader, memoryStream, '\0', true);
pdfStamper.FormFlattening = true;
AcroFields af = reader.AcroFields;
string[] fields = pdfStamper.AcroFields.Fields.Select(x => x.Key).ToArray();
for (int key = 0; key <= fields.Count() - 1; key++)
{
pdfStamper.AcroFields.SetField(fields[key], txtApplicantName.Text);
}
Byte[] bytes= memoryStream.ToArray();
File.WriteAllBytes(@"E:\\Test1.pdf", bytes);
}
有人可以告诉我我做错了吗
答案 0 :(得分:3)
您忘记关闭PdfStamper
。
pdfStamper.Close(); // <===
Byte[] bytes= memoryStream.ToArray();
File.WriteAllBytes(@"E:\\Test1.pdf", bytes);