PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Filling PDF Fields with values in ASP.NET MVC 5
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4223
Page 1 of 1

Author:  CodingHonk [ Fri Jan 15, 2021 11:53 am ]
Post subject:  Filling PDF Fields with values in ASP.NET MVC 5

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?

Author:  TH-Soft [ Mon Jan 18, 2021 7:15 am ]
Post subject:  Re: Filling PDF Fields with values in ASP.NET MVC 5

Answered on SO:
https://stackoverflow.com/a/65726643/162529

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/