title: How do I set the ExportValue of a checkbox using iTextSharp?
body: <p>I'm using VS 2008 Pro &amp; iTextsharp (version 5.1.1).</p>
  
  <p>I have a set of about 30 pdf's. I've managed to use iTextSharp to iterate over all of them and get the export values for the check boxes on the various forms. Some have an export value of On while others have an export value of Yes. I'm trying to find a way to programmatically set the export value of the checkboxes to 'On' so they are all in sync. This is all in an effort for another app that exports pdf's and will sometimes need to mark a checkbox as checked. That application is actually using CeTe's Dynamic Pdf software and the company has some right out on the forums and said their software cannot get the export value of a radio button or chexkbox. With CeTe's software, you mark a checkbox as checked by setting the checkboxes value to the export value. That's a rather large catch; however, if I can use iTextSharp to normalize the exportvalue for all chexkboxes, I can side step this issue.</p>
  
  <p>Anyone have any thoughts?</p>
  
  <p>Btw, here is the code I'm using to get the ExportValue's. It might not be great, but it's functional and intended as a one off program.</p>
  
  <pre><code>    using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.IO;
      using iTextSharp.text.pdf;
  
      namespace iTextSharpTesting1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  Console.WriteLine("App Path: " + LogUtil.logFile);
  
                  DirectoryInfo formsFolder = new DirectoryInfo(@"C:\Development\Forms");
                  string path = string.Empty;
                  int onCount = 0;
                  int yesCount = 0;
                  StringBuilder lines = new StringBuilder(); 
  
                  if( formsFolder != null &amp;&amp; formsFolder.Exists )
                      foreach (FileInfo fi in formsFolder.GetFiles("*.pdf"))
                      {
                          lines.AppendLine(" File Name: " + fi.Name );
  
                          PdfReader reader = new PdfReader(fi.FullName);
                          int counter = 0;
                          using (FileStream fs = new FileStream("Test Out.pdf", FileMode.Create))
                          {
                              PdfStamper stamper = new PdfStamper(reader, fs);
                              AcroFields fields = stamper.AcroFields;
  
                              foreach (string key in fields.Fields.Keys)
                              {
                                  //iTextSharp.text.pdf.AcroFields.Item i = fields.Fields[key];
                                  //bool x = fields.SetFieldProperty("CheckBox1", "exportvalue", "On", null);                            
                                  string[] states = fields.GetAppearanceStates(key);
                                  if (states.Count() &gt; 0)
                                  {
                                      lines.Append(" Field: " + key + "\t" + " State Values: ");
                                      foreach (string s in states)
                                      {
                                          if("on".Equals(s.ToLower() ))
                                              onCount +=1;
                                          else if( "yes".Equals(s.ToLower() ))
                                              yesCount += 1;
  
                                          lines.Append(s + ",");
                                      }
                                      lines.Append("\n");
                                  }
  
                              }
                              //fields.RenameField("oldFieldName", "newFieldName");
                              stamper.Close();
                              LogUtil.Write("Static Void Main", lines.ToString());
                              lines = null;
                              lines = new StringBuilder();
                          }
                      }
  
                  //Added after I noted all ExportValues for the checkboxes seem to be either Yes or On.
                  LogUtil.Write("Static Void Main" , "Yes Count: " + yesCount.ToString() + " On Count: " + onCount.ToString());
              } // end static void main
          } // end class
  
          public class LogUtil
          {
              public static string logFile = @"C:\Development\iTextSharpTest1\iTextSharpTest1\Log\iTextSharpTestingLog.txt";
  
              public LogUtil()
              {
  
              }
  
              public static void WriteBreak()
              {            
                  using (StreamWriter writer = File.AppendText(logFile))
                  {
                      writer.WriteLine("\n*****************************************************************************\n");
                  }
              }
  
              public static void Write(String methodCall, String msg)
              {
                  using (StreamWriter writer = File.AppendText(logFile))
                  {
                      writer.WriteLine("\n" + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() + "\n\t Method: " + methodCall + "\n\t Message:  " + msg);
                  }
              }
  
              public static void WriteException(String methodCall, Exception e)
              {
                  string message = string.Empty;
  
                  if (e == null) return;
  
                  try
                  {
                      message = e.Message;
                      int ctr = 1;
                      while (e != null &amp;&amp; e.InnerException != null)
                      {
                          e = e.InnerException;
                          message += " Inner Error #" + ctr.ToString() + ": " + e.Message;
                          ctr += 1;
                      }
  
                      using (StreamWriter writer = File.AppendText(logFile))
                      {
                          writer.WriteLine("\n************************   BEGIN EXCEPTION LOGGED!!!!     ***********************\n");
                          if (!string.IsNullOrEmpty(methodCall))
                          {
                              writer.WriteLine("\n" + DateTime.Now.ToShortDateString() + " " +
                                  DateTime.Now.ToLongTimeString() + "\n\t Method: " + methodCall +
                                  "\n\t Message:  " + message +
                                  "\n\t stack trace: " + e.StackTrace);
                          }
                          else
                          {
                              writer.WriteLine("\n" + DateTime.Now.ToShortDateString() + " " +
                                  DateTime.Now.ToLongTimeString() +
                                  "\n\t Message:  " + message +
                                  "\n\t stack trace: " + e.StackTrace);
                          }
                          writer.WriteLine("\n************************   END EXCEPTION LOGGED!!!!     ***********************\n");
                      }
                  }
                  catch (Exception ex)
                  {
                      try
                      {
                          LogUtil.Write("", "An attempt to write an exception to the log has failed.");
                      }
                      catch (Exception snuffIt)
                      {
                          //snuffing this exception
                      }
                  }
  
              }
  
              public static void WriteException(Exception e)
              {
                  LogUtil.WriteException(null, e);
              }
  
          }
  
      } //end namespace
  </code></pre>
  
  <p><strong>EDIT 2</strong>
  Here's another oddity...I'm looking over the code use in this question:
  <a href="https://stackoverflow.com/questions/4491156/get-the-export-value-of-a-checkbox-using-itextsharp">Get ExportValue using iTextSharp</a> and have tried to use it in hopes of figuring out an answer to this question. The code used in that question has several PdfDictionary types declared. It's also in java, but that's not a big deal. The problem is that there is no Keys property on the PdfDictionary class for the code I'm using. I've checked the PdfDictionary class in the source project for itextsharp-src-core-5.1.1 and there is very clearly a public property on this class called Keys. Intellisense &amp; the compiler don't seem to see it and I've no idea why. </p>
  
  <p>Obviously the goal here is that I'm trying to iterate over the dictionary and get the values for the various keys, but I'm a bit stumped on how to do that without having access to the Keys in the dictionary used. Granted, I've checked the pdf reference the question linked and found it rather informative. More than likely, the key names will be something like, PdfName.D, PdfName.Yes, PdfName.N, but having an actual list of keys is the best way to figure this out. Any thoughts?</p>