How do I toggle a checkbox widget in PDF? How do I set the 'check' appearance?

Q: I am having difficulties setting check boxes on a PDF form. The
code I am using is as follows:

case Field.Type.e_check:
  if ((value.ToLower().Equals("yes".ToLower())) ||

(value.ToLower().Equals("true".ToLower())) ||
                                (value.ToLower().Equals("0"))) {
else fld.SetValue("");

Setting the field with any value does not toggle the checkbox on the
PDF document. What am I missing here?
A: In PDF the value of a checkbox must correspond to the name of the
appearance stream that should be selected. The appearance for the off
state is optional but, if present, must be stored in the appearance
dictionary under the name "Off". The recommended (but not required)
name for the on state is "Yes".

To set the value of a checkbox widget to false use:

fld.SetValue("Yes") will work in most cases, however since PDF Spec
doesn't guarantee that "Yes" appearance exists it is a better idea to
select the 'checked' appearance as follows:


where GetOnState is a utility function defined as follows:

// C# version (JAVA, VB.NET, C/C++ are along the same lines).
static string GetOnState(Field fld) {
  Obj ap_dict;
  try {
    ap_dict = fld.GetSDFObj().Get("AP").Value().Get("N").Value();
  catch (Exception e) {
    return "Yes";
  DictIterator itr = ap_dict.GetDictIterator();
  for (; itr.HasNext(); itr.Next()) {
    if (itr.Key().GetName().ToLower().Equals("off")) continue;
    else return itr.Key().GetName();
  return "Yes";

Also don't forget to call pdfdoc.RefreshFieldAppearances() just before
saving the updated PDF form:

doc.Save("mypdf.pdf", SDF.SDFDoc.SaveOptions.e_linearized);

If you forget to call RefreshFieldAppearances() the form values will
be correct, however the field appearances may not be in sync with the