How to Create Dropdown Form Field in Word in WPF

Dropdown list in Word is one type of forms that restricts the data entry to an item in the predefined list. Spire.Doc supports to add old dropdown list form field (Legacy Forms) to Word document. This dropdown list is limited to 25 items and requires forms protection. The following section will demonstrate how to create a dropdown form filed in Word in a WPF application.

Code Snippet:

Step 1: Initialize a new instance of Document class, add a section to it.

Document doc = new Document();         
Spire.Doc.Section s = doc.AddSection();

Step 2: Add a paragraph to the section and append text.

Spire.Doc.Documents.Paragraph p = s.AddParagraph();
p.AppendText("Country:  ");

Step 3: Call Paragraph.AppendField() method to insert a dropdown list and call DropDownItems.Add() method to add items.

string fieldName = "DropDownList";
DropDownFormField list = p.AppendField(fieldName, FieldType.FieldFormDropDown) as DropDownFormField;
list.DropDownItems.Add("Italy");
list.DropDownItems.Add("France");
list.DropDownItems.Add("Germany");
list.DropDownItems.Add("Greece");
list.DropDownItems.Add("UK");

Step 4: Add protection to the document by setting the protection type as AllowOnlyFormFields and also setting a password. Note: If you choose not to use a password, anyone can change your editing restrictions.

doc.Protect(ProtectionType.AllowOnlyFormFields,"e-iceblue");

Step 5: Save and launch the file.

doc.SaveToFile("result.doc", FileFormat.Doc);
System.Diagnostics.Process.Start("result.doc");

Output:

How to Create Dropdown Form Field in Word in WPF

Full Code:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Document doc = new Document();
            Spire.Doc.Section s = doc.AddSection();
            Spire.Doc.Documents.Paragraph p = s.AddParagraph();
            p.AppendText("Country:  ");

            ParagraphStyle style = new ParagraphStyle(doc);
            style.Name = "FontStyle";
            style.CharacterFormat.FontName = "Arial";
            style.CharacterFormat.FontSize = 12;
            doc.Styles.Add(style);
            p.ApplyStyle(style.Name);

            string fieldName = "DropDownList";
            DropDownFormField list = p.AppendField(fieldName, FieldType.FieldFormDropDown) as DropDownFormField;
            list.DropDownItems.Add("Italy");
            list.DropDownItems.Add("France");
            list.DropDownItems.Add("Germany");
            list.DropDownItems.Add("Greece");
            list.DropDownItems.Add("UK");

            doc.Protect(ProtectionType.AllowOnlyFormFields, "e-iceblue");
            doc.SaveToFile("result.doc", FileFormat.Doc);
            System.Diagnostics.Process.Start("result.doc");

        }
    }
}