[Test] //ExSkip
        public void Visitor()
        {
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            // Use a document builder to insert a combo box.
            builder.Write("Choose a value from this combo box: ");
            FormField comboBox = builder.InsertComboBox("MyComboBox", new[] { "One", "Two", "Three" }, 0);

            comboBox.CalculateOnExit = true;
            Assert.AreEqual(3, comboBox.DropDownItems.Count);
            Assert.AreEqual(0, comboBox.DropDownSelectedIndex);
            Assert.True(comboBox.Enabled);

            builder.InsertBreak(BreakType.ParagraphBreak);

            // Use a document builder to insert a check box.
            builder.Write("Click this check box to tick/untick it: ");
            FormField checkBox = builder.InsertCheckBox("MyCheckBox", false, 50);

            checkBox.IsCheckBoxExactSize = true;
            checkBox.HelpText            = "Right click to check this box";
            checkBox.OwnHelp             = true;
            checkBox.StatusText          = "Checkbox status text";
            checkBox.OwnStatus           = true;
            Assert.AreEqual(50.0d, checkBox.CheckBoxSize);
            Assert.False(checkBox.Checked);
            Assert.False(checkBox.Default);

            builder.InsertBreak(BreakType.ParagraphBreak);

            // Use a document builder to insert text input form field.
            builder.Write("Enter text here: ");
            FormField textInput = builder.InsertTextInput("MyTextInput", TextFormFieldType.Regular, "", "Placeholder text", 50);

            textInput.EntryMacro       = "EntryMacro";
            textInput.ExitMacro        = "ExitMacro";
            textInput.TextInputDefault = "Regular";
            textInput.TextInputFormat  = "FIRST CAPITAL";
            textInput.SetTextInputValue("New placeholder text");
            Assert.AreEqual(TextFormFieldType.Regular, textInput.TextInputType);
            Assert.AreEqual(50, textInput.MaxLength);

            // This collection contains all of our form fields.
            FormFieldCollection formFields = doc.Range.FormFields;

            Assert.AreEqual(3, formFields.Count);

            // Fields display our form fields. We can see their field codes by opening this document
            // in Microsoft and pressing Alt + F9. These fields have no switches,
            // and members of the FormField object fully govern their form fields' content.
            Assert.AreEqual(3, doc.Range.Fields.Count);
            Assert.AreEqual(" FORMDROPDOWN \u0001", doc.Range.Fields[0].GetFieldCode());
            Assert.AreEqual(" FORMCHECKBOX \u0001", doc.Range.Fields[1].GetFieldCode());
            Assert.AreEqual(" FORMTEXT \u0001", doc.Range.Fields[2].GetFieldCode());

            // Allow each form field to accept a document visitor.
            FormFieldVisitor formFieldVisitor = new FormFieldVisitor();

            using (IEnumerator <FormField> fieldEnumerator = formFields.GetEnumerator())
                while (fieldEnumerator.MoveNext())
                {
                    fieldEnumerator.Current.Accept(formFieldVisitor);
                }

            Console.WriteLine(formFieldVisitor.GetText());

            doc.UpdateFields();
            doc.Save(ArtifactsDir + "FormFields.Visitor.html");
            TestFormField(doc); //ExSkip
        }
Exemple #2
0
        [Test] //ExSkip
        public void FormField()
        {
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            // Use a document builder to insert a combo box
            FormField comboBox = builder.InsertComboBox("MyComboBox", new[] { "One", "Two", "Three" }, 0);

            comboBox.CalculateOnExit = true;
            Assert.AreEqual(3, comboBox.DropDownItems.Count);
            Assert.AreEqual(0, comboBox.DropDownSelectedIndex);
            Assert.True(comboBox.Enabled);

            // Use a document builder to insert a check box
            FormField checkBox = builder.InsertCheckBox("MyCheckBox", false, 50);

            checkBox.IsCheckBoxExactSize = true;
            checkBox.HelpText            = "Right click to check this box";
            checkBox.OwnHelp             = true;
            checkBox.StatusText          = "Checkbox status text";
            checkBox.OwnStatus           = true;
            Assert.AreEqual(50.0d, checkBox.CheckBoxSize);
            Assert.False(checkBox.Checked);
            Assert.False(checkBox.Default);

            builder.Writeln();

            // Use a document builder to insert text input form field
            FormField textInput = builder.InsertTextInput("MyTextInput", TextFormFieldType.Regular, "", "Your text goes here", 50);

            textInput.EntryMacro       = "EntryMacro";
            textInput.ExitMacro        = "ExitMacro";
            textInput.TextInputDefault = "Regular";
            textInput.TextInputFormat  = "FIRST CAPITAL";
            textInput.SetTextInputValue("This value overrides the one we set during initialization");
            Assert.AreEqual(TextFormFieldType.Regular, textInput.TextInputType);
            Assert.AreEqual(50, textInput.MaxLength);

            // Get the collection of form fields that has accumulated in our document
            FormFieldCollection formFields = doc.Range.FormFields;

            Assert.AreEqual(3, formFields.Count);

            // Our form fields are represented as fields, with field codes FORMDROPDOWN, FORMCHECKBOX and FORMTEXT respectively,
            // made visible by pressing Alt + F9 in Microsoft Word
            // These fields have no switches and the content of their form fields is fully governed by members of the FormField object
            Assert.AreEqual(3, doc.Range.Fields.Count);

            // Iterate over the collection with an enumerator, accepting a visitor with each form field
            FormFieldVisitor formFieldVisitor = new FormFieldVisitor();

            using (IEnumerator <FormField> fieldEnumerator = formFields.GetEnumerator())
                while (fieldEnumerator.MoveNext())
                {
                    fieldEnumerator.Current.Accept(formFieldVisitor);
                }

            Console.WriteLine(formFieldVisitor.GetText());

            doc.UpdateFields();
            doc.Save(ArtifactsDir + "Field.FormField.docx");
            TestFormField(doc); //ExSkip
        }