Skip to content

Commit 1bbee7d

Browse files
Merge pull request #384 from SyncfusionExamples/ES-263338-Remove-unmerged-rows-during-mail-merge
ES-263338- Add the sample Remove-unmerged-rows-during-mail-merge
2 parents da1ba7c + f272d90 commit 1bbee7d

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31911.196
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Remove-unmerged-rows-during-mail-merge", "Remove-unmerged-rows-during-mail-merge\Remove-unmerged-rows-during-mail-merge.csproj", "{D3AF529E-DB54-4294-A876-DD42E1E472D0}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {58137FF9-5AE1-4514-9929-3A8A7DA1DFEB}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
7+
namespace Remove_unmerged_rows_during_mail_merge
8+
{
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
14+
{
15+
//Loads an existing Word document into DocIO instance.
16+
using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic))
17+
{
18+
//Gets the employee details as IEnumerable collection.
19+
List<Employee> employeeList = GetEmployees();
20+
21+
//Event to do manipulations when unmerged field occurs in the Word document
22+
document.MailMerge.BeforeClearField += RemoveRowOfDataNotInDataSource;
23+
24+
//Event to remove row when fields have empty value but defined in Datasource
25+
document.MailMerge.MergeField += RemoveRowsOfEmptyValue;
26+
27+
//Creates an instance of MailMergeDataTable by specifying MailMerge group name and IEnumerable collection.
28+
MailMergeDataTable dataSource = new MailMergeDataTable("Employee", employeeList);
29+
//Performs Mail merge.
30+
document.MailMerge.ExecuteGroup(dataSource);
31+
//Creates file stream.
32+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
33+
{
34+
//Saves the Word document to file stream.
35+
document.Save(outputStream, FormatType.Docx);
36+
}
37+
}
38+
}
39+
}
40+
41+
/// <summary>
42+
/// Remove a row if data is not defined in Data source
43+
/// </summary>
44+
private static void RemoveRowOfDataNotInDataSource(object sender, BeforeClearFieldEventArgs args)
45+
{
46+
if (args.GroupName == "Employee" && !args.HasMappedFieldInDataSource)
47+
{
48+
WParagraph paragraph = args.CurrentMergeField.OwnerParagraph;
49+
if (paragraph.IsInCell)
50+
{
51+
WTableRow tableRow = paragraph.Owner.Owner as WTableRow;
52+
(tableRow.Owner as WTable).Rows.Remove(tableRow);
53+
}
54+
}
55+
}
56+
57+
/// <summary>
58+
/// Event handler to remove the row of empty or null value fields
59+
/// </summary>
60+
private static void RemoveRowsOfEmptyValue(object sender, MergeFieldEventArgs args)
61+
{
62+
if (args.GroupName == "Employee" && (args.FieldValue == DBNull.Value || args.FieldValue.ToString() == string.Empty))
63+
{
64+
WParagraph paragraph = args.CurrentMergeField.OwnerParagraph;
65+
if (paragraph.IsInCell)
66+
{
67+
WTableRow tableRow = paragraph.Owner.Owner as WTableRow;
68+
(tableRow.Owner as WTable).Rows.Remove(tableRow);
69+
}
70+
}
71+
}
72+
73+
/// <summary>
74+
/// Gets the employee details to perform mail merge.
75+
/// </summary>
76+
public static List<Employee> GetEmployees()
77+
{
78+
List<Employee> employees = new List<Employee>
79+
{
80+
new Employee("Nancy", "", "722 Moss Bay Blvd.", "USA"),
81+
new Employee("Andrew", "12/12/1988", "", "USA"),
82+
new Employee("Roland", "03/22/1992", "722 Moss Bay Blvd.", "USA"),
83+
new Employee("Margaret", "07/19/1980", "", "USA"),
84+
new Employee("Steven", "09/30/1995", "14 Garrett Hill", "")
85+
};
86+
return employees;
87+
}
88+
}
89+
90+
/// <summary>
91+
/// Represents a class to maintain employee details.
92+
/// </summary>
93+
public class Employee
94+
{
95+
public string Name { get; set; }
96+
public string DOB { get; set; }
97+
public string Address { get; set; }
98+
public string Country { get; set; }
99+
100+
public Employee(string name, string dob, string address, string country)
101+
{
102+
Name = name;
103+
DOB = dob;
104+
Address = address;
105+
Country = country;
106+
}
107+
}
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Remove_unmerged_rows_during_mail_merge</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\Template.docx">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Output\.gitkeep">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
21+
22+
</Project>

0 commit comments

Comments
 (0)