Skip to content

ES-885668- Add the sample Mail-merge-with-two-data-sources #455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31911.196
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4}
EndGlobalSection
EndGlobal
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Mail_merge_with_two_data_sources</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\EmployeesTemplate.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Picture.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using System.Collections.Generic;
using System.Data;
using System.IO;

namespace Mail_merge_with_two_data_sources
{
class Program
{
static void Main(string[] args)
{
// Load the Word document
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/EmployeesTemplate.docx")))
{
//Sets “ClearFields” to true to remove empty mail merge fields from document
document.MailMerge.ClearFields = false;
//Gets the employee details as IEnumerable collection.
List<Employee> employeeList = GetEmployees();
//Creates an instance of MailMergeDataTable by specifying MailMerge group name and IEnumerable collection.
MailMergeDataTable dataSource = new MailMergeDataTable("Employees", employeeList);
//Performs Mail merge.
document.MailMerge.ExecuteGroup(dataSource);

//Uses the mail merge events handler for image fields.
document.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MergeField_LogoImage);
//Gets the DataTable
DataTable dataTable = GetDataTable();
//Performs mail merge to merge the logo
document.MailMerge.Execute(dataTable);

// Save the modified document
document.Save(Path.GetFullPath(@"../../../Output/Result.docx"), FormatType.Docx);
}
}
/// <summary>
/// Gets the employee details to perform mail merge.
/// </summary>
public static List<Employee> GetEmployees()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee("Nancy", "Smith", "Sales Representative", "505 - 20th Ave. E. Apt. 2A,", "Seattle", "WA", "USA"));
employees.Add(new Employee("Andrew", "Fuller", "Vice President, Sales", "908 W. Capital Way", "Tacoma", "WA", "USA"));
employees.Add(new Employee("Roland", "Mendel", "Sales Representative", "722 Moss Bay Blvd.", "Kirkland", "WA", "USA"));
employees.Add(new Employee("Margaret", "Peacock", "Sales Representative", "4110 Old Redmond Rd.", "Redmond", "WA", "USA"));
employees.Add(new Employee("Steven", "Buchanan", "Sales Manager", "14 Garrett Hill", "London", "Kirkland", "UK"));
return employees;
}
/// <summary>
/// Represents the method that handles MergeImageField event.
/// </summary>
private static void MergeField_LogoImage(object sender, MergeImageFieldEventArgs args)
{
//Binds image from file system during mail merge.
if (args.FieldName == "Logo")
{
string photoFileName = args.FieldValue.ToString();
//Gets the image from file system.
FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/" + photoFileName), FileMode.Open, FileAccess.Read);
args.ImageStream = imageStream;
}
}
private static DataTable GetDataTable()
{
//Creates new DataTable instance
DataTable table = new DataTable();
//Add columns in DataTable
table.Columns.Add("Logo");

//Add record in new DataRow
DataRow row = table.NewRow();
row["Logo"] = "Picture.png";
table.Rows.Add(row);

return table;
}
}

/// <summary>
/// Represents a class to maintain employee details.
/// </summary>
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string Country { get; set; }
public string Title { get; set; }
public Employee(string firstName, string lastName, string title, string address, string city, string region, string country)
{
FirstName = firstName;
LastName = lastName;
Title = title;
Address = address;
City = city;
Region = region;
Country = country;
}
}
}
Loading