I want to fill an existing PDF file in my web application (developed in ASP.NET MVC 5). The PDF file has a field named "Text1". In this field I want to write the value "abc". I am currently trying to use PdfSharp for this purpose.
This is how I am currently trying to set a value in a field in the PDF:
Code:
var rDoc = PdfReader.Open(ControllerContext.HttpContext.Server.MapPath(@"~/Documents/test.pdf"), PdfDocumentOpenMode.Modify);
rDoc.AcroForm.Elements.SetValue("/NeedsAppearances", new PdfBoolean(true));
rDoc.AcroForm.Elements.SetValue("/Text1", new PdfString("abc"));
This is how I return the PDF to the client:
Code:
var bytesOfDoc = new byte[0];
using(var ms = new MemoryStream())
{
rDoc.Save(ms, true);
bytesOfDoc = ms.ToArray();
}
return File(bytesOfDoc, "application/pdf", "test.pdf");
But the field "Text1" is always empty. I have already tried various ways. Like for example also this one:
Code:
rDoc.AcroForm.Elements["/Text1"] = new PdfString("abc");
What am I doing wrong?