コード例 #1
0
        public void MailMergeAndConditionalField()
        {
            //ExStart:MailMergeAndConditionalField
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            // Insert a MERGEFIELD nested inside an IF field.
            // Since the IF field statement is false, the result of the inner MERGEFIELD will not be displayed,
            // and the MERGEFIELD will not receive any data during a mail merge.
            FieldIf fieldIf = (FieldIf)builder.InsertField(" IF 1 = 2 ");

            builder.MoveTo(fieldIf.Separator);
            builder.InsertField(" MERGEFIELD  FullName ");

            // We can still count MERGEFIELDs inside false-statement IF fields if we set this flag to true.
            doc.MailMerge.UnconditionalMergeFieldsAndRegions = true;

            DataTable dataTable = new DataTable();

            dataTable.Columns.Add("FullName");
            dataTable.Rows.Add("James Bond");

            doc.MailMerge.Execute(dataTable);

            // The result will not be visible in the document because the IF field is false,
            // but the inner MERGEFIELD did indeed receive data.
            doc.Save(ArtifactsDir + "WorkingWithFields.MailMergeAndConditionalField.docx");
            //ExEnd:MailMergeAndConditionalField
        }
コード例 #2
0
        public static void Run()
        {
            // ExStart:EvaluateIFCondition
            DocumentBuilder builder = new DocumentBuilder();
            FieldIf         field   = (FieldIf)builder.InsertField("IF 1 = 1", null);

            FieldIfComparisonResult actualResult = field.EvaluateCondition();

            Console.WriteLine(actualResult);
            // ExEnd:EvaluateIFCondition

            Console.WriteLine("\nEvaluates the IF condition successfully.");
        }
コード例 #3
0
        public void FieldIf()
        {
            //ExStart
            //ExFor:FieldIf
            //ExFor:FieldIf.ComparisonOperator
            //ExFor:FieldIf.EvaluateCondition
            //ExFor:FieldIf.FalseText
            //ExFor:FieldIf.LeftExpression
            //ExFor:FieldIf.RightExpression
            //ExFor:FieldIf.TrueText
            //ExFor:FieldIfComparisonResult
            //ExSummary:Shows how to insert an if field.
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            builder.Write("Statement 1: ");

            // Use document builder to insert an if field
            FieldIf fieldIf = (FieldIf)builder.InsertField(FieldType.FieldIf, true);

            // The if field will output either the TrueText or FalseText string into the document, depending on the truth of the statement
            // In this case, "0 = 1" is incorrect, so the output will be "False"
            fieldIf.LeftExpression     = "0";
            fieldIf.ComparisonOperator = "=";
            fieldIf.RightExpression    = "1";
            fieldIf.TrueText           = "True";
            fieldIf.FalseText          = "False";

            Assert.AreEqual(" IF  0 = 1 True False", fieldIf.GetFieldCode());
            Assert.AreEqual(FieldIfComparisonResult.False, fieldIf.EvaluateCondition());

            // This time, the statement is correct, so the output will be "True"
            builder.Write("\nStatement 2: ");
            fieldIf = (FieldIf)builder.InsertField(FieldType.FieldIf, true);
            fieldIf.LeftExpression     = "5";
            fieldIf.ComparisonOperator = "=";
            fieldIf.RightExpression    = "2 + 3";
            fieldIf.TrueText           = "True";
            fieldIf.FalseText          = "False";

            Assert.AreEqual(" IF  5 = \"2 + 3\" True False", fieldIf.GetFieldCode());
            Assert.AreEqual(FieldIfComparisonResult.True, fieldIf.EvaluateCondition());

            doc.UpdateFields();
            doc.Save(MyDir + @"\Artifacts\Field.If.docx");
        }