Create PDF forms using PDF4NET.View viewer controls
In this article we'll show how to create PDF forms using the visual form designer capabilities included in the O2S.Component.PDF4NET.View suite of PDF controls (for creating PDF forms from code please see this article).
The PDFDocumentView control provides support for creating PDF forms in a visual manner.

PDF form fields in PDFDocumentView
The form fields in the PDFDocumentView control are represented by the PDFVisualField class and its descendants. These classes encapsulate the coresponding PDFField objects from the standard PDF4NET library, the difference being that PDFVisualField objects trigger an automatic viewer update when their properties are changed.
For example setting the Text property on a PDFVisualTextBoxField object
| C# | |
|---|---|
causes the viewer control to update automatically and display the new value in the field.
The viewer control displays the form fields by default. If you do not want to display the form fields, set the ShowFormFields property to false.
Add a PDF form field to a page
PDFDocumentView supports several interaction modes that let the end user add a form field to the PDF page:
AddTextBoxField- adds a text field onto the page.AddCheckBoxField- adds a checkbox field to the pageAddRadioButtonField- adds a radio button field to the pageAddComboBoxField- adds a combo box field to the pageAddListBoxField- adds a list box field to the pageAddPushButtonField- adds a push-button field to the pageAddSignatureField- adds a signature field to the page
The process of adding a field to the page is the same for all fields: click the mouse on the page to set the position of the field and then drag the mouse to setup the field size. When a form fields is drawn on the page, its default visual appearance (border and background color) is specified by the DefaultFormFieldAppearance property.
After the field has been drawn on the page (the mouse button has been released), the PDFDocumentView raises the BeforeFieldAdd and AfterFieldAdd events.
The BeforeFieldAdd event is raised before the PDFVisualField object is added to the page's VisualFields collection. The event can be used to customize the field's properties (display a dialog to the user to setup various properties) or it can be used to cancel the field's addition if some condition is not true. To cancel the field addition you have to set the AllowAdd property to false in event's arguments.
The AfterFieldAdd event is raised after the PDFVisualField object is added to the page's VisualFields collection. The event can also be used to customize the field's properties (display a dialog to the user to setup various properties).
Edit a PDF form field
Editing existing PDF forms fields is possible when the viewer's active interaction mode is EditFormFields. In this mode fields can be selected on the page to be moved and/or resized. Multiple fields can be selected together by drawing a selection rectangle that covers them or by pressing the CTRL key and clicking on the field.
When one or more fields are selected the AnnotationSelected event is raised (the field's visual representation, the widget, is an annotation). Correspondingly, the AnnotationDeselected event is raised when one or more fields are deselected.
The viewers FormDesigner property is also active in EditFormFields interaction mode and returns the form designer for the current editing session. The form designer provides multiple layout capabilities:
- Align left
- Align center
- Align right
- Align top
- Align middle
- Align bottom
- Center horizontally
- Center vertically
- Center horizontally and vertically
- Distribute horizontally
- Distribute vertically
- Match width
- Match height
- Match width and height
The form fields display a context menu when the user right clicks on them allowing the user to quickly customize the form fields or to delete them. The context menu is show no matter the viewer's active interaction mode. If you do not want the viewer to display the context menu for the form fields, set the viewer's ShowDefaultAnnotationContextMenu property to false.
Delete a PDF form field
The default form field context menu allows the user to delete the form field. When the user selects this option, the PDFDocumentView raises the BeforeAnnotationDelete and AfterAnnotationDelete events (the field's visual representation, the widget, is an annotation).
The BeforeAnnotationDelete event is raised before the form field widget is removed from the PDF file. Use this event to control the form field deletion by setting the boolean AllowDelete property in the event arguments. By default this property is false, you have to handle this event and set it to true if you want the user to be able to delete the form field without any confirmation.
The AfterAnnotationDelete event is raised after the form field widget is removed from the PDF file.
If the form field has a single widget then the form field is also removed from the form when its widget is removed from the page. But if the form field has multiple widgets, the delete operation affects only the clicked widget, the field with its other widgets remain in the PDF file after the clicked widget is removed.