GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API.

Mail Merge Clear Options

GemBox.Document mail merge allows you to remove / clear various parts of the document if mail merge process hasn't imported any data to that part of the document.

Following example demonstrates how to remove paragraph, table row or entire mail merge range if no data has been imported into them.

Screenshot
Clear Options Screenshot

See the full code below, use Run Example to execute.

Upload your file(Drag files here)

Download a sample file

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using GemBox.Document;
using GemBox.Document.MailMerging;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        DocumentModel document = DocumentModel.Load("MailMergeClearOptions.docx");

        // Example 1: Data source will remove "First choice" paragraph because there is no value defined for FirstChoice field.
        var dataSource1 = new
        {
            Header = "My header",
            SecondChoice = "I have chosen second choice."
        };

        document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyParagraphs;
        document.MailMerge.Execute(dataSource1, "Example1");

        // Example 2: Data source will remove table row with label "Address" because value for field Address is null.
        var dataSource2 = new
        {
            Name = "John Doe",
            Email = "john.doe@acme.com",
            Address = (string)null,
            Country = "USA"
        };

        document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyTableRows;
        document.MailMerge.Execute(dataSource2, "Example2");

        // Example 3: Data source will remove mail merge range for second item because it has both Header and Content values that are string.Empty or null.
        var dataSource3 = new
        {
            Count = 2,
            HeaderContent = new object[]
            {
                new 
                {
                    Header = "My header 1",
                    Content = "My content 1.\nSecond line of my content 1."
                },
                new 
                {
                    Header = string.Empty,
                    Content = (object)null
                },
                new 
                {
                    Header = "My header 3",
                    Content = "My content 3.\nSecond line of my content 3."
                }
            }
        };
        document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyRanges | MailMergeClearOptions.RemoveEmptyParagraphs;
        document.MailMerge.Execute(dataSource3, "Example3");            

        document.Save("Clear Options.docx");
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.IO
Imports System.Linq
Imports GemBox.Document
Imports GemBox.Document.MailMerging

Module Samples

    Sub Main()

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

        Dim document As DocumentModel = DocumentModel.Load("MailMergeClearOptions.docx")

        ' Example 1: Data source will remove "First choice" paragraph because there is no value defined for FirstChoice field.
        Dim dataSource1 = New With { _
            .Header = "My header", _
            .SecondChoice = "I have chosen second choice." _
        }

        document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyParagraphs
        document.MailMerge.Execute(dataSource1, "Example1")

        ' Example 2: Data source will remove table row with label "Address" because value for field Address is Nothing.
        Dim dataSource2 = New With { _
            .Name = "John Doe", _
            .Email = "john.doe@acme.com", _
            .Address = DirectCast(Nothing, String), _
            .Country = "USA" _
        }

        document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyTableRows
        document.MailMerge.Execute(dataSource2, "Example2")

        ' Example 3: Data source will remove mail merge range for second item because it has both Header and Content values that are string.Empty or Nothing.
        Dim dataSource3 = New With { _
            .Count = 2, _
            .HeaderContent = New Object() {New With { _
                .Header = "My header 1", _
                .Content = "My content 1." & vbLf & "Second line of my content 1." _
            }, New With { _
                .Header = String.Empty, _
                .Content = DirectCast(Nothing, Object) _
            }, New With { _
                .Header = "My header 3", _
                .Content = "My content 3." & vbLf & "Second line of my content 3." _
            }} _
        }
        document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyRanges Or MailMergeClearOptions.RemoveEmptyParagraphs
        document.MailMerge.Execute(dataSource3, "Example3")

        document.Save("Clear Options.docx")

    End Sub

End Module

Check next sample.