title: Pdf's fields should remain editable using itextsharp in asp.net
body: <p>I have a fillable pdf. In which i have few textboxes.</p>
  
  <p>I fill these fields by using following code(itextsharp).</p>
  
  <pre><code> DataTable dt = new DataTable();
              String pdfPath1 = Server.MapPath("pdfs\\transmittal2.pdf");
              if (File.Exists(pdfPath1))
              {                  
  
                  dt = objClsTransmittal.GetTransmittal(jobid, cid);
                  String comment = "Correspondence generated for " + dt.Rows[0]["Recipient"].ToString();                  
                  var formfield = PDFHelper.GetFormFieldNames(pdfPath1);
                  formfield["DocDate"] = DateTime.Now.ToLongDateString();
                  formfield["Address1"] = dt.Rows[0]["Company"].ToString();
                  formfield["Address2"] = dt.Rows[0]["Address1"].ToString();
                  formfield["PropertyAddress"] = dt.Rows[0]["PropertyAddress"].ToString();
                  formfield["Job"] = dt.Rows[0]["JobID"].ToString();
                  formfield["Name"] = dt.Rows[0]["Recipient"].ToString();
                  formfield["CityStateZip"] = dt.Rows[0]["address2"].ToString();
                  formfield["E-mail"] = dt.Rows[0]["Email"].ToString();
                  var pdfcontent = PDFHelper.GeneratePDF(pdfPath1, formfield);                    
                  PDFHelper.ReturnPDF(pdfcontent, "Transmittal.pdf");
  
              }
  </code></pre>
  
  <p>Currently its downloded as read only pdf.</p>
  
  <p>when this pdf gets downloaded, i want that all fields still remain fillable, with the text i have filled in pdf. So that i can edit the text.</p>
  
  <p>I'm looking forward for your replies.</p>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>PdfHelper is my custom class. In which i have used following code:</p>
  
  <pre><code>  using System;
     using System.Collections.Generic;
    using System.Collections;
    using System.Linq;
    using System.Web;
     using System.IO;
     using iTextSharp.text.pdf;
  
    public class PDFHelper
    {
      public static Dictionary&lt;string, string&gt; GetFormFieldNames(string pdfPath)
      {
      var fields = new Dictionary&lt;string, string&gt;();
  
      var reader = new PdfReader(pdfPath);
      foreach (DictionaryEntry entry in reader.AcroFields.Fields)
          fields.Add(entry.Key.ToString(), string.Empty);
      reader.Close();
  
      return fields;
  }
  
  public static byte[] GeneratePDF(string pdfPath, Dictionary&lt;string, string&gt; formFieldMap)
  {
      var output = new MemoryStream();
      var reader = new PdfReader(pdfPath);
      var stamper = new PdfStamper(reader, output);
      var formFields = stamper.AcroFields;
  
      foreach (var fieldName in formFieldMap.Keys)
          formFields.SetField(fieldName, formFieldMap[fieldName]);
  
      stamper.FormFlattening = true;
      stamper.Close();
      reader.Close();
  
      return output.ToArray();
  }
  
  
  public static string GetExportValue(AcroFields.Item item)
  {
      var valueDict = item.GetValue(0);
      var appearanceDict = valueDict.GetAsDict(PdfName.AP);
  
      if (appearanceDict != null)
      {
          var normalAppearances = appearanceDict.GetAsDict(PdfName.N);
  
          if (normalAppearances != null)
          {
              foreach (var curKey in normalAppearances.Keys)
                  if (!PdfName.OFF.Equals(curKey))
                      return curKey.ToString().Substring(1); // string will have a leading '/' character, so remove it!
          }
      }
  
  
      var curVal = valueDict.GetAsName(PdfName.AS);
      if (curVal != null)
          return curVal.ToString().Substring(1);
      else
          return string.Empty;
  }
  
  public static void ReturnPDF(byte[] contents)
  {
      ReturnPDF(contents, null);
  }
  
  public static void ReturnPDF(byte[] contents, string attachmentFilename)
  {
      var response = HttpContext.Current.Response;
  
      if (!string.IsNullOrEmpty(attachmentFilename))
          response.AddHeader("Content-Disposition", "attachment; filename=" + attachmentFilename);
  
      response.ContentType = "application/pdf";
      response.BinaryWrite(contents);
      response.End();
  }
  </code></pre>