Skip to content

Commit ed5995b

Browse files
ES-885668-mail-merge-with-two-data-sources
1 parent efb219e commit ed5995b

File tree

6 files changed

+153
-0
lines changed

6 files changed

+153
-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}") = "Mail-merge-with-two-data-sources", "Mail-merge-with-two-data-sources\Mail-merge-with-two-data-sources.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
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+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.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 = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Mail_merge_with_two_data_sources</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\EmployeesTemplate.docx">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Data\Picture.png">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
<None Update="Output\.gitkeep">
21+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
22+
</None>
23+
</ItemGroup>
24+
25+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.Collections.Generic;
4+
using System.Data;
5+
using System.IO;
6+
7+
namespace Mail_merge_with_two_data_sources
8+
{
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
// Load the Word document
14+
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/EmployeesTemplate.docx")))
15+
{
16+
//Sets “ClearFields” to true to remove empty mail merge fields from document
17+
document.MailMerge.ClearFields = false;
18+
//Gets the employee details as IEnumerable collection.
19+
List<Employee> employeeList = GetEmployees();
20+
//Creates an instance of MailMergeDataTable by specifying MailMerge group name and IEnumerable collection.
21+
MailMergeDataTable dataSource = new MailMergeDataTable("Employees", employeeList);
22+
//Performs Mail merge.
23+
document.MailMerge.ExecuteGroup(dataSource);
24+
25+
//Uses the mail merge events handler for image fields.
26+
document.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MergeField_LogoImage);
27+
//Gets the DataTable
28+
DataTable dataTable = GetDataTable();
29+
//Performs mail merge to merge the logo
30+
document.MailMerge.Execute(dataTable);
31+
32+
// Save the modified document
33+
document.Save(Path.GetFullPath(@"../../../Output/Result.docx"), FormatType.Docx);
34+
}
35+
}
36+
/// <summary>
37+
/// Gets the employee details to perform mail merge.
38+
/// </summary>
39+
public static List<Employee> GetEmployees()
40+
{
41+
List<Employee> employees = new List<Employee>();
42+
employees.Add(new Employee("Nancy", "Smith", "Sales Representative", "505 - 20th Ave. E. Apt. 2A,", "Seattle", "WA", "USA"));
43+
employees.Add(new Employee("Andrew", "Fuller", "Vice President, Sales", "908 W. Capital Way", "Tacoma", "WA", "USA"));
44+
employees.Add(new Employee("Roland", "Mendel", "Sales Representative", "722 Moss Bay Blvd.", "Kirkland", "WA", "USA"));
45+
employees.Add(new Employee("Margaret", "Peacock", "Sales Representative", "4110 Old Redmond Rd.", "Redmond", "WA", "USA"));
46+
employees.Add(new Employee("Steven", "Buchanan", "Sales Manager", "14 Garrett Hill", "London", "Kirkland", "UK"));
47+
return employees;
48+
}
49+
/// <summary>
50+
/// Represents the method that handles MergeImageField event.
51+
/// </summary>
52+
private static void MergeField_LogoImage(object sender, MergeImageFieldEventArgs args)
53+
{
54+
//Binds image from file system during mail merge.
55+
if (args.FieldName == "Logo")
56+
{
57+
string photoFileName = args.FieldValue.ToString();
58+
//Gets the image from file system.
59+
FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/" + photoFileName), FileMode.Open, FileAccess.Read);
60+
args.ImageStream = imageStream;
61+
}
62+
}
63+
private static DataTable GetDataTable()
64+
{
65+
//Creates new DataTable instance
66+
DataTable table = new DataTable();
67+
//Add columns in DataTable
68+
table.Columns.Add("Logo");
69+
70+
//Add record in new DataRow
71+
DataRow row = table.NewRow();
72+
row["Logo"] = "Picture.png";
73+
table.Rows.Add(row);
74+
75+
return table;
76+
}
77+
}
78+
79+
/// <summary>
80+
/// Represents a class to maintain employee details.
81+
/// </summary>
82+
public class Employee
83+
{
84+
public string FirstName { get; set; }
85+
public string LastName { get; set; }
86+
public string Address { get; set; }
87+
public string City { get; set; }
88+
public string Region { get; set; }
89+
public string Country { get; set; }
90+
public string Title { get; set; }
91+
public Employee(string firstName, string lastName, string title, string address, string city, string region, string country)
92+
{
93+
FirstName = firstName;
94+
LastName = lastName;
95+
Title = title;
96+
Address = address;
97+
City = city;
98+
Region = region;
99+
Country = country;
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)