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.

Clear Options Screenshot
Upload your file (Drag file here)
using System;
using GemBox.Document;
using GemBox.Document.MailMerging;

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

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

        // 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.%OutputFileType%");
    }
}
Imports System
Imports GemBox.Document
Imports GemBox.Document.MailMerging

Module Program

    Sub Main()

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

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

        ' 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.%OutputFileType%")

    End Sub

End Module

Check next example or download examples from GitHub.