Itext Sharp Merge Pdfs with acrofields
asp.net-mvc, c#, itext, pdf
Solution
I found a solution to this problem thanks to this answer by rhens
All I had to do is modify my GeneratePdf function by adding one line:
form.GenerateAppearances = true;
Here is the end result:
private static byte[] GeneratePdf(Dictionary<String, String> formKeys, String pdfPath)
{
var templatePath = System.Web.HttpContext.Current.Server.MapPath(pdfPath);
var reader = new PdfReader(templatePath);
var outStream = new MemoryStream();
var stamper = new PdfStamper(reader, outStream);
var form = stamper.AcroFields;
form.GenerateAppearances = true; //Added this line, fixed my problem
var fieldKeys = form.Fields.Keys;
foreach (KeyValuePair<String, String> pair in formKeys)
{
if (fieldKeys.Any(f => f == pair.Key))
{
form.SetField(pair.Key, pair.Value);
}
}
stamper.Close();
reader.Close();
return flattenPdf(outStream.ToArray());
}
and the flattenPdf stays the same as in my question.
Problem
I'm using itext sharp to fill my form fields on my template with values. I created the template using pdfescape.com Here is my code that I use to place the values in the pdf template. ``` private static byte[] GeneratePdf(Dictionary<String, String> formKeys, String pdfPath) { var templatePath = System.Web.HttpContext.Current.Server.MapPath(pdfPath); var reader = new PdfReader(templatePath); var outStream = new MemoryStream(); var stamper = new PdfStamper(reader, outStream); var form = stamper.AcroFields; var fieldKeys = form.Fields.Keys; // "Flatten" the form so it wont be editable/usable anymore // stamper.FormFlattening = true; foreach (KeyValuePair<String, String> pair in formKeys) { if (fieldKeys.Any(f => f == pair.Key)) { form.SetField(pair.Key, pair.Value); form.SetFieldProperty(pair.Key, "setfflags", PdfFormField.FF_READ_ONLY, null); } } stamper.Close(); reader.Close(); return outStream.ToArray(); } ``` I first used the stamper.FormFlattening = true, but then the values weren't visible. So Instead of using the form flattening I just set the values as ready only and everything works fine. Now I want to merge multiple of these pdf files using the the pdf merger by smart-soft Once the merging is complete the values aren't visible. When I highlight over the form it highlights all the text, but I can't read it.I did research on this and read that the fields need to be flattened. Here is an image of how it looks on the pdf when I highlight everything: I don't know why my fields aren't visible when they are flattened, even if I don't use the merger. Is there something wrong with the code or the template? Alternatives will also be appreciated. Btw my project is an asp-mvc project if that is relevant. EDIT I added the following code so that I first read the template, write the values to the form fields, close it, reopen it, flatten and then close it again as suggested by one of the comments. I just pass the result I get from the GeneratePdf function to this function: ``` private static byte[] flattenPdf(byte[] pdf) { var reader = new PdfReader(pdf); var outStream = new MemoryStream(); var stamper = new PdfStamper(reader, outStream); stamper.FormFlattening = true; stamper.Close(); reader.Close(); return outStream.ToArray(); } ``` I still get the same result