How to control of Fields/ Forms using C# and VB.NET

Here you will find how create a document with interactive Form and Fields controls.
Let's start using a template document containg some merge field (ContactName, Address, CompanyName). In the first step, we'll fill these fields by our data. In the second step, we'll add some form contols to our document: CheckBox, TextBox and DropDown list.

 

Main steps:

  1. Notice you are importing the SautinSoft.Document namespace.
    using SautinSoft.Document;
  2. We have a simple DOCX file (Template) with and several Merge Fields wich we want to fill. ( The instruction How to insert a Merge Field in a document).
    
                        CompanyName = "Picture gallery of USA",
                        Address = "720 Sacramento St, San Francisco, CA 94108",
                        PrintDate = DateTime.Now.ToLongDateString(),
                        PaintingNumber = "123/5-68",
                        ContactName = "Mr. Bean"
    


  3. Here we add a date with several options to our resulting document.


  4. The nest step is adding form fields: TextBox, CheckBox and DropDown. Our controls are: Full Name, BirthDate, Gender, Married, Phone.

  5.             
                // Create form fields.
    
                Field fFullName = new Field(dc, FieldType.FormText, null, placeHolder);
                fFullName.FormData.Name = "FullName";
                fFullName.FormData.Enabled = true;
    
    
                Field fBirthData = new Field(dc, FieldType.FormText, null, placeHolder);
                fBirthData.FormData.Name = "BirthDate";
    
                Field fGender = new Field(dc, FieldType.FormDropDown);
                fGender.FormData.Name = "Gender";
    
                Field fMarried = new Field(dc, FieldType.FormCheckBox);
                fMarried.FormData.Name = "Married";
                fMarried.FormData.Enabled = true;
    
    
                Field fPhone = new Field(dc, FieldType.FormText, null, placeHolder);
                fPhone.FormData.Name = "Phone";
    
                dc.Sections.Add(new Section(dc,
    
                    new Paragraph(dc,
                    new Run(dc, "Full name: "),
                    fFullName),
    
                    new Paragraph(dc,
                    new Run(dc, "Birth date: "),
                    fBirthData),
    
                    new Paragraph(dc,
                    new Run(dc, "Gender: "),
                    fGender),
    
                    new Paragraph(dc,
                    new Run(dc, "Married: "),
                    fMarried),
    
                    new Paragraph(dc,
                    new Run(dc, "Phone: "),
                    fPhone)));
    
  6. We have created form fields and now customize form fields. For example: Gender.
                
                var genderFieldData = (FormDropDownData)formFieldsData["Gender"];
                genderFieldData.Items.Add("Select sex");
                genderFieldData.Items.Add("Male");
                genderFieldData.Items.Add("Female");
                genderFieldData.Items.Add("I don't know");
                genderFieldData.StatusText = genderFieldData.HelpText =
                    "Select your gender.";
                genderFieldData.SelectedItemIndex = 0;
    
  7. Well done. Let's save our document to PDF format.
  8. The input Template: fields-template.docx
    The output result: fields-result.pdf
 

Complete code

using System;
using System.Globalization;
using System.Text;
using SautinSoft.Document;
using SautinSoft.Document.MailMerging;
using SautinSoft.Document.Tables;

class Sample
{
    static void Main(string[] args)
    {
        FormsAndFields();
    }
    public static void FormsAndFields()
    {
        DocumentCore dc = DocumentCore.Load(@"..\..\..\..\..\..\Testing Files\fields-template.docx");

        // Fill document header.
        dc.MailMerge.Execute(
                new
                {
                    CompanyName = "Picture gallery of USA",
                    Address = "720 Sacramento St, San Francisco, CA 94108",
                    PrintDate = DateTime.Now.ToLongDateString(),
                    PaintingNumber = "123/5-68",
                    ContactName = "Mr. Bean"
                });

        // Options for writing a date.
        dc.Sections.Add(
            new Section(dc,
                   new Paragraph(dc,
                    new Run(dc, "Date: "),
                    // { DATE }
                    new Field(dc, FieldType.Date),
                    new SpecialCharacter(dc, SpecialCharacterType.LineBreak),
                    new Run(dc, "Date (formatted): "),
                    // { DATE \@ "dddd, MMMM dd, yyyy"  \* MERGEFORMAT }
                    new Field(dc, FieldType.Date, "\\@ \"dddd, MMMM dd, yyyy\"  \\* MERGEFORMAT"),
                    new SpecialCharacter(dc, SpecialCharacterType.LineBreak),
                    new Run(dc, "Date & Time (formatted): "),
                    // { DATE  \@ "M/d/yyyy h:mm:ss am/pm"  \* MERGEFORMAT }
                    new Field(dc, FieldType.Date, " \\@ \"M/d/yyyy h:mm:ss am/pm\"  \\* MERGEFORMAT"))));

        string placeHolder = new string('\x2002', 50);

        // Create form fields.

        Field fFullName = new Field(dc, FieldType.FormText, null, placeHolder);
        fFullName.FormData.Name = "FullName";
        fFullName.FormData.Enabled = true;


        Field fBirthData = new Field(dc, FieldType.FormText, null, placeHolder);
        fBirthData.FormData.Name = "BirthDate";

        Field fGender = new Field(dc, FieldType.FormDropDown);
        fGender.FormData.Name = "Gender";

        Field fMarried = new Field(dc, FieldType.FormCheckBox);
        fMarried.FormData.Name = "Married";
        fMarried.FormData.Enabled = true;


        Field fPhone = new Field(dc, FieldType.FormText, null, placeHolder);
        fPhone.FormData.Name = "Phone";

        dc.Sections.Add(new Section(dc,

            new Paragraph(dc,
            new Run(dc, "Full name: "),
            fFullName),

            new Paragraph(dc,
            new Run(dc, "Birth date: "),
            fBirthData),

            new Paragraph(dc,
            new Run(dc, "Gender: "),
            fGender),

            new Paragraph(dc,
            new Run(dc, "Married: "),
            fMarried),

            new Paragraph(dc,
            new Run(dc, "Phone: "),
            fPhone)));


        // Customize form fields.
        var formFieldsData = dc.Content.FormFieldsData;

        var fullNameFieldData = (FormTextData)formFieldsData["FullName"];
        fullNameFieldData.MaximumLength = 50;
        fullNameFieldData.StatusText = fullNameFieldData.HelpText = "Enter your name and surname (trimmed to 50 characters).";
        fullNameFieldData.Field.ResultInlines.Content.Replace("Mister Bean");

        var birthdateFieldData = (FormTextData)formFieldsData["BirthDate"];
        birthdateFieldData.TextType = FormTextType.Date;
        birthdateFieldData.DefaultValue = "1990-01-01";
        birthdateFieldData.ValueFormat = "yyyy-MM-dd";
        birthdateFieldData.StatusText = birthdateFieldData.HelpText =
            "Enter your date of birth.";
        birthdateFieldData.Field.ResultInlines.Content.Replace("1990-01-01");

        var genderFieldData = (FormDropDownData)formFieldsData["Gender"];
        genderFieldData.Items.Add("Select sex");
        genderFieldData.Items.Add("Male");
        genderFieldData.Items.Add("Female");
        genderFieldData.Items.Add("I don't know");
        genderFieldData.StatusText = genderFieldData.HelpText =
            "Select your gender.";
        genderFieldData.SelectedItemIndex = 0;

        var marriedFieldData = (FormCheckBoxData)formFieldsData["Married"];
        marriedFieldData.StatusText = marriedFieldData.HelpText =
                    "Mark as checked if you are married.";
        marriedFieldData.DefaultValue = true;
        marriedFieldData.Value = true;

        var salaryFieldData = (FormTextData)formFieldsData["Phone"];
        salaryFieldData.TextType = FormTextType.Number;
        salaryFieldData.DefaultValue = "555 13-12";
        salaryFieldData.ValueFormat = "(###) ###-####";
        salaryFieldData.StatusText = salaryFieldData.HelpText =
            "Enter your phone number.";
        salaryFieldData.Field.ResultInlines.Content.Replace("+1 (800) 111 2233");

        dc.Save(@"fields-template.pdf", new PdfSaveOptions() { PreserveFormFields = true });

        // Open the result for demonstation purposes.
        System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(@"fields-template.pdf") { UseShellExecute = true });
    }
}
        
            Imports SautinSoft.Document

Module ExampleVB

    Sub Main()
        FormsAndFields()
    End Sub
    Sub FormsAndFields()
        Dim dc As DocumentCore = DocumentCore.Load("..\..\..\..\..\Testing Files\fields-template.docx")

        ' Fill document header.
        dc.MailMerge.Execute(New With {
            Key .CompanyName = "Picture gallery of USA",
            Key .Address = "720 Sacramento St, San Francisco, CA 94108",
            Key .PrintDate = Date.Now.ToLongDateString(),
            Key .PaintingNumber = "123/5-68",
            Key .ContactName = "Mr. Bean"
        })

        ' Options for writing a date.
        dc.Sections.Add(New Section(dc, New Paragraph(dc, New Run(dc, "Date: "), New Field(dc, FieldType.Date), New SpecialCharacter(dc, SpecialCharacterType.LineBreak), New Run(dc, "Date (formatted): "), New Field(dc, FieldType.Date, "\@ ""dddd, MMMM dd, yyyy""  \* MERGEFORMAT"), New SpecialCharacter(dc, SpecialCharacterType.LineBreak), New Run(dc, "Date & Time (formatted): "), New Field(dc, FieldType.Date, " \@ ""M/d/yyyy h:mm:ss am/pm""  \* MERGEFORMAT"))))
        ' { DATE }
        ' { DATE \@ "dddd, MMMM dd, yyyy"  \* MERGEFORMAT }
        ' { DATE  \@ "M/d/yyyy h:mm:ss am/pm"  \* MERGEFORMAT }

        Dim placeHolder As New String(ChrW(&H2002), 50)

        ' Create form fields.

        Dim fFullName As New Field(dc, FieldType.FormText, Nothing, placeHolder)
        fFullName.FormData.Name = "FullName"
        fFullName.FormData.Enabled = True


        Dim fBirthData As New Field(dc, FieldType.FormText, Nothing, placeHolder)
        fBirthData.FormData.Name = "BirthDate"

        Dim fGender As New Field(dc, FieldType.FormDropDown)
        fGender.FormData.Name = "Gender"

        Dim fMarried As New Field(dc, FieldType.FormCheckBox)
        fMarried.FormData.Name = "Married"
        fMarried.FormData.Enabled = True


        Dim fPhone As New Field(dc, FieldType.FormText, Nothing, placeHolder)
        fPhone.FormData.Name = "Phone"

        dc.Sections.Add(New Section(dc, New Paragraph(dc, New Run(dc, "Full name: "), fFullName), New Paragraph(dc, New Run(dc, "Birth date: "), fBirthData), New Paragraph(dc, New Run(dc, "Gender: "), fGender), New Paragraph(dc, New Run(dc, "Married: "), fMarried), New Paragraph(dc, New Run(dc, "Phone: "), fPhone)))


        ' Customize form fields.
        Dim formFieldsData = dc.Content.FormFieldsData

        Dim fullNameFieldData = CType(formFieldsData("FullName"), FormTextData)
        fullNameFieldData.MaximumLength = 50
        'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
        'ORIGINAL LINE: fullNameFieldData.StatusText = fullNameFieldData.HelpText = "Enter your name and surname (trimmed to 50 characters).";
        fullNameFieldData.HelpText = "Enter your name and surname (trimmed to 50 characters)."
        fullNameFieldData.StatusText = fullNameFieldData.HelpText
        fullNameFieldData.Field.ResultInlines.Content.Replace("Mister Bean")

        Dim birthdateFieldData = CType(formFieldsData("BirthDate"), FormTextData)
        birthdateFieldData.TextType = FormTextType.Date
        birthdateFieldData.DefaultValue = "1990-01-01"
        birthdateFieldData.ValueFormat = "yyyy-MM-dd"
        'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
        'ORIGINAL LINE: birthdateFieldData.StatusText = birthdateFieldData.HelpText = "Enter your date of birth.";
        birthdateFieldData.HelpText = "Enter your date of birth."
        birthdateFieldData.StatusText = birthdateFieldData.HelpText
        birthdateFieldData.Field.ResultInlines.Content.Replace("1990-01-01")

        Dim genderFieldData = CType(formFieldsData("Gender"), FormDropDownData)
        genderFieldData.Items.Add("Select sex")
        genderFieldData.Items.Add("Male")
        genderFieldData.Items.Add("Female")
        genderFieldData.Items.Add("I don't know")
        'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
        'ORIGINAL LINE: genderFieldData.StatusText = genderFieldData.HelpText = "Select your gender.";
        genderFieldData.HelpText = "Select your gender."
        genderFieldData.StatusText = genderFieldData.HelpText
        genderFieldData.SelectedItemIndex = 0

        Dim marriedFieldData = CType(formFieldsData("Married"), FormCheckBoxData)
        'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
        'ORIGINAL LINE: marriedFieldData.StatusText = marriedFieldData.HelpText = "Mark as checked if you are married.";
        marriedFieldData.HelpText = "Mark as checked if you are married."
        marriedFieldData.StatusText = marriedFieldData.HelpText
        marriedFieldData.DefaultValue = True
        marriedFieldData.Value = True

        Dim salaryFieldData = CType(formFieldsData("Phone"), FormTextData)
        salaryFieldData.TextType = FormTextType.Number
        salaryFieldData.DefaultValue = "555 13-12"
        salaryFieldData.ValueFormat = "(###) ###-####"
        'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
        'ORIGINAL LINE: salaryFieldData.StatusText = salaryFieldData.HelpText = "Enter your phone number.";
        salaryFieldData.HelpText = "Enter your phone number."
        salaryFieldData.StatusText = salaryFieldData.HelpText
        salaryFieldData.Field.ResultInlines.Content.Replace("+1 (800) 111 2233")

        dc.Save("fields-template.pdf", New PdfSaveOptions() With {.PreserveFormFields = True})

        ' Open the result for demonstation purposes.
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("fields-template.pdf") With {.UseShellExecute = True})
    End Sub
End Module

          
© SautinSoft 2019