Reputation: 47
As per my previous post ITextsharp to edit existing pdf I am able to save the PDF in interactive manner. After that I started writing the following code to modify the content and save it to a PDF as follow but when I am trying to open the file it is saying There was an error opening in the document. The file is damaged and could not be repaired
This is my code
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);
}
Can some one tell me where I am doing wrong
Upvotes: 1
Views: 94
Reputation: 96064
You forgot to close the PdfStamper
.
pdfStamper.Close(); // <===
Byte[] bytes= memoryStream.ToArray();
File.WriteAllBytes(@"E:\\Test1.pdf", bytes);
Upvotes: 3