Determine widget type and access widget from form field

Product:
Apryse SDK C#
Product Version:
11.2

I have 2 questions for which I could not find answer in the sdk doc or samples.

  1. How can I determine whether an annotation is actually a textwidget or a checkbox etc. Calling annot.GetType () == e_Widget tells me that it is a widget but I’d like to know the exact type.
  2. I have a form Field object. How can I access the corresponding widget?

Thx.

1 Like

Great question!

You can actually “cast” between Widget and Field classes with the same Obj.

So to iterate Widgets and get to the Field you would do something like the following.

for (PageIterator itr = doc.GetPageIterator(); itr.HasNext(); itr.Next())
{
	Page page = itr.Current();
	int num_annots = page.GetNumAnnots();
	for (int i = 0; i < num_annots; ++i)
	{
		Annot annot = page.GetAnnot(i);
		if (!annot.IsValid()) continue;

		if (annot.GetType() == Annot.Type.e_Widget)
		{
			Field field = new Field(annot.GetSDFObj());
			Field.Type type = field.GetType();
		}
	}
}

And to go the other way, from Field to Widget you would try the following.

FieldIterator itr;
for(itr=doc.GetFieldIterator(); itr.HasNext(); itr.Next()) 
{
	Field field = itr.Current();
	try
	{
		// Fields don't have to have a Widget (e.g. a Author/certification digital signature field).
		// So we want to wrap this in a catch block.
		Widget widget = new Widget(field.GetSDFObj());
	}
	catch (PDFNetException e)
	{
		Console.WriteLine(e.Message);
	}
}
1 Like

Thank you for your quick reply Ryan! In your first code snippet the field type value is not 100% perfect because both ComboBoxWidget and ListBoxWidget have a field type of e_choice. Is there a way to distinguish between these two? For other types of Widgets it is fine.

1 Like

Great question, you would need to check a flag, like this.

if(field.GetType() == e_choice) {
  //If e_combo is set, the field is a combo box; if clear, the field is a list box.
  bool isCombo = field.GetFlag(e_combo);
}
1 Like