Customize the GUI interaction of a radio button

UI, View, PDF forms
5/28/2014

Downloads

This sample shows how one can create a custom radio button interactor that responds to other key strokes than just the space bar.

Custom Annotation Interactor

In PDFControls.NET, interactive GUI elements that deal with PDF elements are called interactors. They are similar to Controls in WinForms. If one needs to have some special kind of handling in the GUI for some PDF element, the best way to do this is often to install a custom interactor for that element.

Below, we have defined a custom radio button interactor. It is a subclass of the standard RadioButtonInteractor that PDFControls.NET creates for all radio buttons in a PDF document. It overrides the OnKeyUp() method, so that it implements additional behavior when pressing the enter key. In this case, it will toggle the value of the radio box when the enter key goes up. If other keys are pressed the default behavior gets invoked by calling base.OnKeyUp().

Radio buttons in PDF are basically implemented as a number of radio button widgets that all refer to the same radio button field. The appearance of each widget depends on the value of the field. Each widget has an ExportName associated with it. Only when this name matches the value of the field, the widget will appear 'checked'. This means that if one sets the value of the field to the export name of a particular widget, all the other radio button widgets will be cleared.

It is also possible to clear all radio button widgets by setting the value of the radio button field to null.

1 class MyRadioButtonInteractor : RadioButtonInteractor 2 { 3 public MyRadioButtonInteractor(RadioButtonWidget widget) 4 : base(widget) 5 { 6 } 7 8 protected override void 9 OnKeyUp(TallComponents.Interaction.WinForms.Events.KeyboardEventArgs e) 10 { 11 if (e.KeyData == System.Windows.Forms.Keys.Enter) 12 { 13 RadioButtonField radioButtonField = Widget.Field as RadioButtonField; 14 RadioButtonWidget radioButtonWidget = Widget as RadioButtonWidget; 15 16 if (radioButtonField != null && radioButtonWidget != null) 17 { 18 if (radioButtonField.RadioButtonValue == null || 19 radioButtonField.RadioButtonValue.ExportName != radioButtonWidget.Option.ExportName) 20 { 21 radioButtonField.RadioButtonValue = radioButtonWidget.Option; 22 } 23 else 24 { 25 radioButtonField.RadioButtonValue = null; 26 } 27 } 28 } 29 else 30 { 31 base.OnKeyUp(e); 32 } 33 } 34 }

Custom Interactor Factory

Installing such a special interactor goes via a custom factory. In this case one needs a custom annotation factory, as a radio button is basically a special kind of annotation in PDF. The modified factory takes the standard annotation factory as its base and just overrides the CreateRadioButtonInteractor method, as that is the only type of annotation that we need a special interactor for. It simply returns an instance of the custom interactor that we have defined above.

1 public class MyAnnotationFactory : AnnotationInteractorFactory 2 { 3 protected override WinFormsInteractor CreateRadioButtonInteractor(RadioButtonWidget radioButtonWidget) 4 { 5 return new MyRadioButtonInteractor(radioButtonWidget); 6 } 7 }

It is possible to create custom interactors for many types of PDF annotations in this way. The base AnnotationFactory currently defines the following overridable create methods:

  • CreateCheckBoxInteractor
  • CreateDateTimeTextBoxInteractor
  • CreateDropDownListInteractor
  • CreateFreeTextInteractor
  • CreateImageFieldInteractor
  • CreateLineInteractor
  • CreateLinkInteractor
  • CreateListBoxInteractor
  • CreateMultiLineTextBoxInteractor
  • CreateNoteInteractor
  • CreateNumericTextBoxInteractor
  • CreatePasswordTextBoxInteractor
  • CreatePopupInteractor
  • CreatePushButtonInteractor
  • CreateRadioButtonInteractor
  • CreateSignatureInteractor
  • CreateSingleLineTextBoxInteractor
  • CreateSquareInteractor
  • CreateStampMarkupInteractor
  • CreateStampMarkupInteractor
  • CreateTextMarkupInteractor
  • CreateUnknownMarkupInteractor

This custom annotation factory in turn can be installed by assigning it to the AnnotationInteractorFactory property of the pages viewer, as shown below. This will make sure that the appropriate annotation interactors get created each time that a page becomes visible that has some annotations on it

1 public partial class Form1 : Form 2 { 3 public Form1() 4 { 5 InitializeComponent(); 6 pagesViewer1.AnnotationInteractorFactory = new MyAnnotationFactory(); 7 } 8 9 protected override void OnLoad(EventArgs e) 10 { 11 base.OnLoad(e); 12 document1.Open(@"..\..\radio.pdf"); 13 } 14 }

Please note that an interactor can be created multiple times for the same PDF annotation. If a page becomes invisible, the interactors on it may be removed at some point to conserve memory. If then later the page becomes visible again, the appropriate create methods of the annotation factory will be called again to create new interactors for the annotations.