How do I remove all form field annotations with the same same?


I have a pdf document with a submit and print button. If there is only
one set of submit and print buttons on a given page, I am successfully
able to remove them using the following code:

Field printButton =
      Annot print_annot = new Annot(printButton.GetSDFObj());

      //grc Remove Submit Button
      Field submitButton =
      Annot submit_annot = new Annot(submitButton.GetSDFObj());

I have a couple of forms where the submit and print buttons appear at
the top and bottom of the page - with the same form field name. Using
the above code code, only the bottom references are removed.

How do I remove all instances of form field annotations with the same

If you have several form-field annotations with the same name, you will
need to call AnnotRemove multiple times.
The following example illustrates how you could implement this

static void RemoveAllFields(PDFDoc doc, Page page, String field_name) {
   FieldIterator itr = doc.FieldFind(field_name);
   for (int counter=0; itr!= doc.FieldEnd();
     itr=doc.FieldFind(field_name), ++counter) {
       Field f = itr.Current();
       // check if the field is a valid annotation (i.e. must have
Subtype key)
       if (f.GetSDFObj().FindObj("Subtype") != null) {
          // Get the annotation from the Field...
          Annot annot = new Annot(f.GetSDFObj());

Example of use:
RemoveAllFields(pdfdoc, pdfdoc.PageFind(page_num).Current(),

I tried this logic, but the process appears to be hung-up in an
infinite loop trying to find and delete two fields. Any assistance is
greatly appreciated.

there may be multiple fields on different pages. As a result the old
loop will not be able to remove all fields because they are on
different pages.

Instead you could use the following snippet to remove all Widget
annotations matching a given name on the supplied page:

static void RemoveAllFieldsWithThisName(Page page, String field_name)
  int num_annots = page.GetNumAnnots();
  // Traverse page annotations in reverse order
  for (int i=num_annots-1; i>=0; --i) {
    Annot annot = page.GetAnnot(i);
    if(annot.GetType() == Annot.Type.e_Widget) {
      Field fld = annot.GetWidgetField();
      if (fld.GetName() == field_name) {