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:
var doc = PdfReader.Open(ControllerContext.HttpContext.Server.MapPath("~/Documents/test.pdf"), PdfDocumentOpenMode.Modify);
doc.AcroForm.Fields["Text1"].ReadOnly = false;
That was the preparation.
And after that I tried various things. For example, this:
doc.AcroForm.Elements.SetValue("Text1", "abc");
But this did not work, because string
cannot be converted to PdfSharp.Pdf.PdfItem
.
I also tried this:
doc.AcroForm.Fields["Text1"].Value = "abc";
Again, I get the same exception as above.
Is there anyway to set the values of the fields of an existing PDF? Does this work with PdfSharp?
doc.AcroForm.Elements.SetValue("Text1", new PdfString("abc"));
It seems there are a variety of classes that derive fromPdfItem
(PdfString
,PdfInteger
, etc) - just a guess. – Violaceous