How to create an IF field in C#

Usually we need to display different text and message to our readers based on the different conditions. In such situation, we need to create if field to decide which result to be displayed to readers. This article focuses on show you how to create an IF Field in C# with the help of Spire.Doc for .NET. We use the IF field and MERGEFIELD field together.

{IF { MERGEFIELD Count } > "100" "Thanks" "The minimum order is 100 units"}

Step 1: Create a new word document.

Document document = new Document();

Step 2: Add a new section for the document.

Section section = document.AddSection();

Step 3: Add a new paragraph for the section.

Paragraph paragraph = section.AddParagraph();

Step 4: Define a method of creating an IF Field.

CreateIfField(document, paragraph);

Step 5: Define merged data.

string[] fieldName = {"Count"};
string[] fieldValue = { "2" };

Step 6: Merge data into the IF Field.

document.MailMerge.Execute(fieldName, fieldValue);

Step 7: Update all fields in the document.

document.IsUpdateFields = true;

Step 8: Save the document to file.

document.SaveToFile("sample.docx", FileFormat.Docx);

The following CreateIfField() method shows how to create the IF Field like:

{IF { MERGEFIELD Count } > "100" "Thanks" " The minimum order is 100 units "}

static void CreateIfField(Document document, Paragraph paragraph)
{         
IfField ifField = new IfField(document);
ifField.Type = FieldType.FieldIf;
ifField.Code = "IF ";
paragraph.Items.Add(ifField);

paragraph.AppendField("Count",FieldType.FieldMergeField);
paragraph.AppendText(" > ");
paragraph.AppendText("\"100\" ");
paragraph.AppendText("\"Thanks\" ");
paragraph.AppendText("\"The minimum order is 100 units\"");

IParagraphBase end = document.CreateParagraphItem(ParagraphItemType.FieldMark);
(end as FieldMark).Type = FieldMarkType.FieldEnd;
paragraph.Items.Add(end);
ifField.End = end as FieldMark;
}

Check the effective screenshot as below:

How to create an IF field in C#

Full Code:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Interface;
namespace CreatIF
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            Section section = document.AddSection();
            Paragraph paragraph = section.AddParagraph();
            CreateIfField(document, paragraph);
            string[] fieldName = { "Count" };
            string[] fieldValue = { "2" };

            document.MailMerge.Execute(fieldName, fieldValue);
            document.IsUpdateFields = true;
            document.SaveToFile("sample.docx", FileFormat.Docx);
        }

        static void CreateIfField(Document document, Paragraph paragraph)
        {
            IfField ifField = new IfField(document);
            ifField.Type = FieldType.FieldIf;
            ifField.Code = "IF ";
            paragraph.Items.Add(ifField);
            paragraph.AppendField("Count", FieldType.FieldMergeField);
            paragraph.AppendText(" > ");
            paragraph.AppendText("\"100\" ");
            paragraph.AppendText("\"Thanks\" ");
            paragraph.AppendText("\"The minimum order is 100 units\"");
            IParagraphBase end = document.CreateParagraphItem(ParagraphItemType.FieldMark);
            (end as FieldMark).Type = FieldMarkType.FieldEnd;
            paragraph.Items.Add(end);
            ifField.End = end as FieldMark;
        }

    }
}