Replacing text field widgets with checkboxes.

Q:

We need to remove text field from a PDF form and add a new field of
type checkbox in the same position using PDFNet SDK 3.5.

The new field does get added in the correct place on the form, however
all attempts to set the old one to invisible or remove it are ignored.
Likewise, we can't set the new checkbox to have any colour property so
it never shows up. Inititially it is behind the old field, but it
still has no colour.

Are we missing a step? c# code indicating the issue is below.

// i denotes an integer within loop through all annotations on page
Annot annot = page.GetAnnot(i);
Field field = annot.GetWidgetField();

string fieldName = field.GetName();

//try using iterator to find and remove the old field
FieldIterator fieldItr = doc.FieldFind(fieldName);
Field oldField = fieldItr.Current();
oldField.SetFlag(Field.Flag.e_read_only,true);

//try renaming it?!?...this worked..but it still needs to be removed
oldField.Rename(string.Format("{0}_old",fieldName));

//try setting it to hidden? This didn't work
annot.SetFlag(Annot.Flag.e_hidden,true);

//Try removing the whole annotation? No!
page.AnnotRemove(annot);

//Try refreshing the fields appearance? No!
oldField.RefreshAppearance();

//Create the new checkbox field with the same name as the original
field...worked after renaming old one
Field newField =
doc.FieldCreate(fieldName,pdftron.PDF.Field.Type.e_check);

Annot newAnnot = pdftron.PDF.Annot.CreateWidget(doc,annot.GetRect(),
newField);

//Set the border style so we have a colour so we can see it! No!
Annot.BorderStyle bs = new Annot.BorderStyle(1);
newAnnot.SetBorderStyle(bs);

//set the colour too..No!
newAnnot.SetColor(new ColorPt(0,0,1));

//try example code from pdftron help?
newAnnot.SetAppearance(CreateCheckmarkAppearance(doc),
Annot.AnnotationState.e_normal);

page.AnnotInsert(i,newAnnot);
-------------------
A:

You should first rename the form field, then remove the old annotation,
then create the new field & annotation, and then set the new annotation
appearance.

The only tricky point is to keep the number of annotations and the
iterator 'i' in sync when removing annotations.

int num_annots = doc.GetNumAnnots();
for (int i=0; i<num_annots; ++i) {
Annot annot = page.GetAnnot(i);
// Check if this is a widget annotation if (annot.GetType() ==
Annot.Type.e_Widget) {
  // rename the form field
  Field field = annot.GetWidgetField();
  string fieldName = field.GetName();
  RenameAllFields(doc, string.Format("{0}_old", fieldName));

  // Then remove the actual annotation from the page
  page.AnnotRemove(annot);
  // Keep in mind that annotation array is invalidated after a
  // a call to AnnotRemove() so you will need to reset the size of
  // array and annotation iterator (if you are traversing all
  // annoatations on the page).
  num_annots = num_annots - 1;
  i = i - 1;

  //Create the new checkbox field with the same name as the original
field
  Field newField =
doc.FieldCreate(fieldName,pdftron.PDF.Field.Type.e_check);
  Annot newAnnot = pdftron.PDF.Annot.CreateWidget(doc,annot.GetRect(),
newField);
  newAnnot.SetAppearance(CreateCheckmarkAppearance(doc),
Annot.AnnotationState.e_normal);
  page.AnnotPushBack(newAnnot);
  // to avoid the infinite loop we don't increment 'num_annots' and 'i'
here.
}