|
| 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