Customize the UI interaction of a check box

UI, PDF forms


This sample shows how one can create a custom check box 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 check box interactor. It is a subclass of the standard CheckBoxInteractor that PDFControls.NET creates for all check boxes 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 check box when the enter key goes up. If other keys are pressed the default behavior gets invoked by calling base.OnKeyUp().

1 class MyCheckBoxInteractor : CheckBoxInteractor 2 { 3 public MyCheckBoxInteractor(CheckBoxWidget widget) 4 : base(widget) 5 { 6 } 7 8 protected override void OnKeyUp(TallComponents.Interaction.WinForms.Events.KeyboardEventArgs e) 9 { 10 if (e.KeyData == System.Windows.Forms.Keys.Enter) 11 { 12 CheckBoxField checkBoxField = Widget.Field as CheckBoxField; 13 14 if (checkBoxField != null) 15 { 16 switch (checkBoxField.CheckBoxValue) 17 { 18 case CheckState.On: 19 checkBoxField.CheckBoxValue = CheckState.Off; 20 break; 21 default: 22 checkBoxField.CheckBoxValue = CheckState.On; 23 break; 24 } 25 } 26 } 27 else 28 { 29 base.OnKeyUp(e); 30 } 31 } 32 }

Custom Interactor Factory

Installing such a special interactor goes via a custom factory. In this case one needs a custom annotation factory, as a check box is basically a special kind of annotation in PDF. The modified factory takes the standard annotation factory as its base and just overrides the CreateCheckBoxInteractor 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 CreateCheckBoxInteractor(CheckBoxWidget checkBoxWidget) 4 { 5 return new MyCheckBoxInteractor(checkBoxWidget); 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(@"..\..\check.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.