title: Itext Sharp Merge Pdfs with acrofields
body: <p>I'm using itext sharp to fill my form fields on my template with values.</p>
  
  <p>I created the template using <a href="http://www.pdfescape.com/" rel="nofollow noreferrer">pdfescape.com</a></p>
  
  <p>Here is my code that I use to place the values in the pdf template.</p>
  
  <pre><code> private static byte[] GeneratePdf(Dictionary&lt;String, String&gt; 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&lt;String, String&gt; pair in formKeys)
          {
              if (fieldKeys.Any(f =&gt; 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();
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>Now I want to merge multiple of these pdf files using the <a href="http://www.codeproject.com/Articles/28283/Simple-NET-PDF-Merger" rel="nofollow noreferrer">the pdf merger by smart-soft</a></p>
  
  <p>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.</p>
  
  <p>Here is an image of how it looks on the pdf when I highlight everything:
  <img src="https://i.stack.imgur.com/4oaJD.jpg" alt="Image of pdf highlighted Text"></p>
  
  <p>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.</p>
  
  <p>Btw my project is an asp-mvc project if that is relevant.</p>
  
  <p><strong><em>EDIT</em></strong></p>
  
  <p>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:</p>
  
  <pre><code>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();
      }
  </code></pre>
  
  <p>I still get the same result</p>