Skip to content
Guides

Fill Form Fields

ComPDFKit supports programmatically filling form fields in a PDF document.

Since form fields are a special type of annotation, inheriting from the annotation class, the interface for annotations applies to form fields.

The steps to fill form fields using code are as follows:

  1. Obtain the page object from CPDFDocument where you want to fill in the form.

  2. Retrieve all annotations from the page object.

  3. Iterate through all annotations to find the form to be filled.

  4. Modify the form field content as needed.

This example shows how to fill form fields:

C#
CPDFDocument document = CPDFDocument.InitWithFilePath("filePath");
CPDFPage page = document.PageAtIndex(0);

List<CPDFAnnotation> annotList = page.GetAnnotations();
foreach (CPDFAnnotation annot in annotList)
{
    if (annot.Type==C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
    {
        CPDFWidget widget = annot as CPDFWidget;
        switch(widget.WidgeType)
        {
            case C_WIDGET_TYPE.WIDGET_TEXTFIELD:
                {
                    CPDFTextWidget text = widget as CPDFTextWidget;
                    text.SetText("test");
                    text.UpdateFormAp();
                }
                break;

            case C_WIDGET_TYPE.WIDGET_RADIOBUTTON:
                {
                    CPDFRadioButtonWidget radio = widget as CPDFRadioButtonWidget;
                    radio.SetChecked(true);
                    radio.UpdateFormAp();
                }
                break;

            case C_WIDGET_TYPE.WIDGET_LISTBOX:
                {
                    CPDFListBoxWidget listBox = widget as CPDFListBoxWidget;
                    listBox.SelectItem(0);
                    listBox.UpdateFormAp();
                }
                break;

            default:
                break;
        }
    }
}