Skip to content
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-in-multi-column-page", "Mail-merge-in-multi-column-page\Mail-merge-in-multi-column-page.csproj", "{D3AF529E-DB54-4294-A876-DD42E1E472D0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {58137FF9-5AE1-4514-9929-3A8A7DA1DFEB}
EndGlobalSection
EndGlobal
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

<ItemGroup>
<None Update="Data\Andrew.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Janet.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Margaret.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Nancy.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Steven.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Template.docx">
<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,95 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using System.Collections.Generic;
using System.IO;

namespace Mail_merge_in_multi_column_page
{
class Program
{
static void Main(string[] args)
{
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
{
//Loads an existing Word document into DocIO instance.
using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic))
{
//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);
//Uses the mail merge events handler for image fields.
document.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MergeField_EmployeeImage);
//Performs Mail merge.
document.MailMerge.ExecuteGroup(dataSource);
//Creates file stream.
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
{
//Saves the Word document to file stream.
document.Save(outputStream, 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", "Nancy.png"));
employees.Add(new Employee("Andrew", "Fuller", "Vice President, Sales", "908 W. Capital Way", "Tacoma", "WA", "USA", "Andrew.png"));
employees.Add(new Employee("Roland", "Mendel", "Sales Representative", "722 Moss Bay Blvd.", "Kirkland", "WA", "USA", "Janet.png"));
employees.Add(new Employee("Margaret", "Peacock", "Sales Representative", "4110 Old Redmond Rd.", "Redmond", "WA", "USA", "Margaret.png"));
employees.Add(new Employee("Steven", "Buchanan", "Sales Manager", "14 Garrett Hill", "London", string.Empty, "UK", "Steven.png"));
return employees;
}

/// <summary>
/// Represents the method that handles MergeImageField event.
/// </summary>
private static void MergeField_EmployeeImage(object sender, MergeImageFieldEventArgs args)
{
//Binds image from file system during mail merge.
if (args.FieldName == "Photo")
{
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;
//Gets the picture, to be merged for image merge field
WPicture picture = args.Picture;
//Resizes the picture
picture.Height = 90;
picture.Width = 90;
}
}
}

/// <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 string Photo { get; set; }
public Employee(string firstName, string lastName, string title, string address, string city, string region, string country, string photoFilePath)
{
FirstName = firstName;
LastName = lastName;
Title = title;
Address = address;
City = city;
Region = region;
Country = country;
Photo = photoFilePath;
}
}
}
Loading