Set "Commit selected value immediately" property for ComboBox field and ListBox field

When we operate the ComboBox field and ListBox field on PDF files, we can set the options for Dropdown Properties. This article will focus on how you how to set "Commit selected value immediately" property for ComboBox field and ListBox field. We will divide into two parts for how to set the property of "Commit selected value immediately", one is for the existing field on PDF and the other is for the new field added to the PDF file.

The following code is for how to set "Commit selected value immediately" property for the existing ListBox field on PDF:

//create a PDF document and load the document from file
 PdfDocument pdf = new PdfDocument();
 pdf.LoadFromFile("FormField.pdf");

 //get the existing form
 PdfFormWidget fw = pdf.Form as PdfFormWidget;

 //get the ListBox field and set the field of CommitOnSelChange property as true
 if (fw != null)
 {
     for (int i = 0; i < fw.FieldsWidget.Count; i++)
     {
         if (fw.FieldsWidget[i] != null)
         {
             if (fw.FieldsWidget[i] is PdfListBoxWidgetFieldWidget)
             {

                 (fw.FieldsWidget[i] as PdfListBoxWidgetFieldWidget).CommitOnSelChange = true;
             }
         }
     }
 }
 pdf.SaveToFile("Result.pdf");

Set

The following code is for how to set the property of "Commit selected value immediately" for ComboBox field when we add a new ComboBox field on PDF:

using (PdfDocument pdf = new PdfDocument())
{
    PdfPageBase page = pdf.Pages.Add();
    
     //add a new ComboBox Field and add data to it
PdfComboBoxField combo = new PdfComboBoxField(page, "ComboBox");
    combo.Bounds = new RectangleF(20, 20,60, 20);
    PdfListFieldItem item1 = new PdfListFieldItem("First", "1");
    PdfListFieldItem item2 = new PdfListFieldItem("Second", "2");
    combo.Items.Add(item1);
    combo.Items.Add(item2);

//set the field of CommitOnSelChange property as true
    combo.CommitOnSelChange = true;

    pdf.Form.Fields.Add(combo);
    pdf.SaveToFile("Result2.Pdf");
}

Set