Skip to content

Commit 9c76e10

Browse files
Merge pull request #333 from SyncfusionExamples/Mail-merge-in-multiple-column
ES-828339- Add sample Mail-merge-in-multiple-column
2 parents 335888a + 53b52aa commit 9c76e10

File tree

10 files changed

+158
-0
lines changed

10 files changed

+158
-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-in-multi-column-page", "Mail-merge-in-multi-column-page\Mail-merge-in-multi-column-page.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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Mail_merge_in_multi_column_page</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\Andrew.png">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Data\Janet.png">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
<None Update="Data\Margaret.png">
21+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
22+
</None>
23+
<None Update="Data\Nancy.png">
24+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
25+
</None>
26+
<None Update="Data\Steven.png">
27+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
28+
</None>
29+
<None Update="Data\Template.docx">
30+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
31+
</None>
32+
<None Update="Output\.gitkeep">
33+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
34+
</None>
35+
</ItemGroup>
36+
37+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
6+
namespace Mail_merge_in_multi_column_page
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
13+
{
14+
//Loads an existing Word document into DocIO instance.
15+
using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic))
16+
{
17+
//Gets the employee details as IEnumerable collection.
18+
List<Employee> employeeList = GetEmployees();
19+
//Creates an instance of MailMergeDataTable by specifying MailMerge group name and IEnumerable collection.
20+
MailMergeDataTable dataSource = new MailMergeDataTable("Employees", employeeList);
21+
//Uses the mail merge events handler for image fields.
22+
document.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MergeField_EmployeeImage);
23+
//Performs Mail merge.
24+
document.MailMerge.ExecuteGroup(dataSource);
25+
//Creates file stream.
26+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
27+
{
28+
//Saves the Word document to file stream.
29+
document.Save(outputStream, FormatType.Docx);
30+
}
31+
}
32+
}
33+
}
34+
35+
/// <summary>
36+
/// Gets the employee details to perform mail merge.
37+
/// </summary>
38+
public static List<Employee> GetEmployees()
39+
{
40+
List<Employee> employees = new List<Employee>();
41+
employees.Add(new Employee("Nancy", "Smith", "Sales Representative", "505 - 20th Ave. E. Apt. 2A,", "Seattle", "WA", "USA", "Nancy.png"));
42+
employees.Add(new Employee("Andrew", "Fuller", "Vice President, Sales", "908 W. Capital Way", "Tacoma", "WA", "USA", "Andrew.png"));
43+
employees.Add(new Employee("Roland", "Mendel", "Sales Representative", "722 Moss Bay Blvd.", "Kirkland", "WA", "USA", "Janet.png"));
44+
employees.Add(new Employee("Margaret", "Peacock", "Sales Representative", "4110 Old Redmond Rd.", "Redmond", "WA", "USA", "Margaret.png"));
45+
employees.Add(new Employee("Steven", "Buchanan", "Sales Manager", "14 Garrett Hill", "London", string.Empty, "UK", "Steven.png"));
46+
return employees;
47+
}
48+
49+
/// <summary>
50+
/// Represents the method that handles MergeImageField event.
51+
/// </summary>
52+
private static void MergeField_EmployeeImage(object sender, MergeImageFieldEventArgs args)
53+
{
54+
//Binds image from file system during mail merge.
55+
if (args.FieldName == "Photo")
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+
//Gets the picture, to be merged for image merge field
62+
WPicture picture = args.Picture;
63+
//Resizes the picture
64+
picture.Height = 90;
65+
picture.Width = 90;
66+
}
67+
}
68+
}
69+
70+
/// <summary>
71+
/// Represents a class to maintain employee details.
72+
/// </summary>
73+
public class Employee
74+
{
75+
public string FirstName { get; set; }
76+
public string LastName { get; set; }
77+
public string Address { get; set; }
78+
public string City { get; set; }
79+
public string Region { get; set; }
80+
public string Country { get; set; }
81+
public string Title { get; set; }
82+
public string Photo { get; set; }
83+
public Employee(string firstName, string lastName, string title, string address, string city, string region, string country, string photoFilePath)
84+
{
85+
FirstName = firstName;
86+
LastName = lastName;
87+
Title = title;
88+
Address = address;
89+
City = city;
90+
Region = region;
91+
Country = country;
92+
Photo = photoFilePath;
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)