If Fields

GemBox.Document provides support for conditional mail merging with IF fields.

By using IF fields you can compare two text values in the mail merge process and merge or insert the appropriate result text.

The following example shows how you can perform a conditional mail merge and get resolved IF field results.

Word document with MERGEFIELD and IF fields
Screenshot of Word file with MERGEFIELD and IF fields
Word document generated from conditional mail merge process
Screenshot of Word file created with conditional mail merge
Upload your file (Drag file here)
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var document = DocumentModel.Load("%InputFileName%");

        var data = new
        {
            FirstName = "John",
            LastName = "Doe",
            Gender = "Male",
            Age = 30
        };

        document.MailMerge.Execute(data);

        document.Save("Merged If Fields Output.%OutputFileType%");
    }
}
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document = DocumentModel.Load("%InputFileName%")

        Dim data = New With
        {
            .FirstName = "John",
            .LastName = "Doe",
            .Gender = "Male",
            .Age = 30
        }

        document.MailMerge.Execute(data)

        document.Save("Merged If Fields Output.%OutputFileType%")

    End Sub
End Module

The IF field's code has the following syntax:

{ IF "Value1" Operator "Value2" "TrueText" "FalseText" }

Each part, except Operator, can contain text and other Field elements. After executing a mail merge process, the result is either TrueText or <FalseText, based on the used comparison operator.

Operator Description
= Equal
<> Not equal
< Less than
> Greater than
<= Less than or equal
>= Greater than or equal

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Document yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.