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 sample 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)

1using System;
2using System.Collections.Generic;
3using System.Data;
4using System.IO;
5using System.Linq;
6using GemBox.Document;
7using GemBox.Document.MailMerging;
8
9class Sample
10{
11    [STAThread]
12    static void Main(string[] args)
13    {
14        // If using Professional version, put your serial key below.
15        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
16
17        DocumentModel document = DocumentModel.Load("MailMergeClearOptions.docx");
18
19        // Example 1: Data source will remove "First choice" paragraph because there is no value defined for FirstChoice field.
20        var dataSource1 = new
21        {
22            Header = "My header",
23            SecondChoice = "I have chosen second choice."
24        };
25
26        document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyParagraphs;
27        document.MailMerge.Execute(dataSource1, "Example1");
28
29        // Example 2: Data source will remove table row with label "Address" because value for field Address is null.
30        var dataSource2 = new
31        {
32            Name = "John Doe",
33            Email = "john.doe@acme.com",
34            Address = (string)null,
35            Country = "USA"
36        };
37
38        document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyTableRows;
39        document.MailMerge.Execute(dataSource2, "Example2");
40
41        // 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.
42        var dataSource3 = new
43        {
44            Count = 2,
45            HeaderContent = new object[]
46            {
47                new 
48                {
49                    Header = "My header 1",
50                    Content = "My content 1.\nSecond line of my content 1."
51                },
52                new 
53                {
54                    Header = string.Empty,
55                    Content = (object)null
56                },
57                new 
58                {
59                    Header = "My header 3",
60                    Content = "My content 3.\nSecond line of my content 3."
61                }
62            }
63        };
64        document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyRanges | MailMergeClearOptions.RemoveEmptyParagraphs;
65        document.MailMerge.Execute(dataSource3, "Example3");            
66
67        document.Save("Clear Options.docx");
68    }
69}
1Imports System
2Imports System.Collections.Generic
3Imports System.Data
4Imports System.IO
5Imports System.Linq
6Imports GemBox.Document
7Imports GemBox.Document.MailMerging
8
9Module Samples
10
11    Sub Main()
12
13        ' If using Professional version, put your serial key below.
14        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
15
16        Dim document As DocumentModel = DocumentModel.Load("MailMergeClearOptions.docx")
17
18        ' Example 1: Data source will remove "First choice" paragraph because there is no value defined for FirstChoice field.
19        Dim dataSource1 = New With { _
20            .Header = "My header", _
21            .SecondChoice = "I have chosen second choice." _
22        }
23
24        document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyParagraphs
25        document.MailMerge.Execute(dataSource1, "Example1")
26
27        ' Example 2: Data source will remove table row with label "Address" because value for field Address is Nothing.
28        Dim dataSource2 = New With { _
29            .Name = "John Doe", _
30            .Email = "john.doe@acme.com", _
31            .Address = DirectCast(Nothing, String), _
32            .Country = "USA" _
33        }
34
35        document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyTableRows
36        document.MailMerge.Execute(dataSource2, "Example2")
37
38        ' 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.
39        Dim dataSource3 = New With { _
40            .Count = 2, _
41            .HeaderContent = New Object() {New With { _
42                .Header = "My header 1", _
43                .Content = "My content 1." & vbLf & "Second line of my content 1." _
44            }, New With { _
45                .Header = String.Empty, _
46                .Content = DirectCast(Nothing, Object) _
47            }, New With { _
48                .Header = "My header 3", _
49                .Content = "My content 3." & vbLf & "Second line of my content 3." _
50            }} _
51        }
52        document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyRanges Or MailMergeClearOptions.RemoveEmptyParagraphs
53        document.MailMerge.Execute(dataSource3, "Example3")
54
55        document.Save("Clear Options.docx")
56
57    End Sub
58
59End Module

Check next sample or find out more about GemBox.Document and GemBox Software.