title: How to use iTextSharp to populate radio button
body: <p>I have a table and one of the column is <code>YesNo</code> which is of type <code>varchar(3)</code>. I have two radio buttons in my PDF file which is in an array:</p>
  
  <pre><code>pg1rb (radio group field)
   --&gt; c1 (radio button one (yes))
   --&gt; c2 (radio button two (no))
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/9ZRJZ.png" alt="enter image description here"></p>
  
  <p>I set textboxes like this in my code:</p>
  
  <pre><code>formFieldMap["topmostSubform[0].Page1[0].f1_01_0_[0]"] = reader.GetValue(0).ToString();
  </code></pre>
  
  <p>How can I select YES or NO radio button based on the row value?</p>
  
  <p>Can I do something like this:</p>
  
  <pre><code>formFieldMap["pg1rb"] = reader.GetBoolean(10); //10 is the 9th column which is Yes/No value
  </code></pre>
  
  <p>Or do I get the value and based on it select the radio button, something like this:</p>
  
  <pre><code>if (reader.GetValue(10).ToString() == "Yes") {
       formFieldMap["pg1rb"][c1] = "1";
  else {
       formFieldMap["pg1rb"][c2] = "1";
  }
  </code></pre>
  
  <p>My SQL table:</p>
  
  <p><img src="https://i.stack.imgur.com/SgCh9.png" alt="enter image description here"></p>
  
  <p>I am trying to follow this site: <a href="https://web.archive.org/web/20211020001747/https://www.4guysfromrolla.com/articles/030211-1.aspx" rel="nofollow noreferrer">Website example</a></p>
  
  <p>My function that actually does the conversion:</p>
  
  <pre><code>public void writeData(string k, string c)
      {
          Conn = new SqlConnection(cString);
          Conn.Open();
  
          //MessageBox.Show(k);
          //MessageBox.Show(c);
  
          var pdfPath = Path.Combine(Server.MapPath("~/PDFTemplates/forme.pdf"));
  
          // Get the form fields for this PDF and fill them in!
          var formFieldMap = PDFHelper.GetFormFieldNames(pdfPath);
  
          //if more than multiple entries, verify by name and the last four ssn
          sqlCode = "SELECT * FROM [DSPCONTENT01].[dbo].[TablePDFTest] WHERE [name] = '" + k + "' AND [ssn3] = " + c + "";
          //sqlCode = "SELECT * FROM [DSPCONTENT01].[dbo].[TablePDFTest] WHERE [name] = @name2 AND [ssn3] = @ssnnum";
          //MessageBox.Show("" + sqlCode.ToString());
  
          using (SqlCommand command = new SqlCommand(sqlCode, Conn))
          {
              command.CommandType = CommandType.Text;
              //command.Parameters.AddWithValue("name2", k);
              //command.Parameters.AddWithValue("ssnnum", c);
  
              using (reader = command.ExecuteReader())
              {
                  if (reader.HasRows)
                  {
                      if (reader.Read())
                      {
                          //MessageBox.Show(reader.GetValue(0).ToString());
                          /*formFieldMap["topmostSubform[0].Page1[0].f1_01_0_[0]"] = reader.GetValue(0).ToString();
                          formFieldMap["topmostSubform[0].Page1[0].f1_02_0_[0]"] = reader.GetValue(1).ToString();
                          formFieldMap["topmostSubform[0].Page1[0].f1_04_0_[0]"] = reader.GetValue(2).ToString();
                          formFieldMap["topmostSubform[0].Page1[0].f1_05_0_[0]"] = reader.GetValue(3).ToString();
                          formFieldMap["topmostSubform[0].Page1[0].f1_07_0_[0]"] = reader.GetValue(4).ToString();
                          formFieldMap["topmostSubform[0].Page1[0].social[0].TextField1[0]"] = reader.GetValue(5).ToString();
                          formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[0]"] = reader.GetValue(6).ToString();
                          formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[1]"] = reader.GetValue(7).ToString();
                          formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[2]"] = reader.GetValue(8).ToString();
                          formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[3]"] = reader.GetValue(9).ToString();*/
                          if (reader.GetValue(10).ToString() == "Yes")
                          {
                              //MessageBox.Show("YES");
                          }
                          else if (reader.GetValue(10).ToString() == "No")
                          {
                              //MessageBox.Show("NO");
                          }
                      }
                  }
              }
          }
  
          // Requester's name and address (hard-coded)
          formFieldMap["topmostSubform[0].Page1[0].f1_06_0_[0]"] = "Medical Group\n12 Westchester Ave\nPurchase, NY 10121";
  
          var pdfContents = PDFHelper.GeneratePDF(pdfPath, formFieldMap);
  
          PDFHelper.ReturnPDF(pdfContents, "Completed-W9.pdf");
      }
  </code></pre>
  
  <p>The PDFHelper class:</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 = false;
          stamper.Close();
          reader.Close();
  
          return output.ToArray();
      }
  
      // See http://stackoverflow.com/questions/4491156/get-the-export-value-of-a-checkbox-using-itextsharp/
      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);
              // /D is for the "down" appearances.
  
              // if there are normal appearances, one key will be "Off", and the other
              // will be the export value... there should only be two.
              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!
              }
          }
  
          // if that doesn't work, there might be an /AS key, whose value is a name with 
          // the export value, again with a leading '/', 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>
  
  <p><img src="https://i.stack.imgur.com/J4yrQ.png" alt="enter image description here"></p>