|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | + |
| 7 | +namespace Remove_unmerged_rows_during_mail_merge |
| 8 | +{ |
| 9 | + class Program |
| 10 | + { |
| 11 | + static void Main(string[] args) |
| 12 | + { |
| 13 | + using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite)) |
| 14 | + { |
| 15 | + //Loads an existing Word document into DocIO instance. |
| 16 | + using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic)) |
| 17 | + { |
| 18 | + //Gets the employee details as IEnumerable collection. |
| 19 | + List<Employee> employeeList = GetEmployees(); |
| 20 | + |
| 21 | + //Event to do manipulations when unmerged field occurs in the Word document |
| 22 | + document.MailMerge.BeforeClearField += RemoveRowOfDataNotInDataSource; |
| 23 | + |
| 24 | + //Event to remove row when fields have empty value but defined in Datasource |
| 25 | + document.MailMerge.MergeField += RemoveRowsOfEmptyValue; |
| 26 | + |
| 27 | + //Creates an instance of MailMergeDataTable by specifying MailMerge group name and IEnumerable collection. |
| 28 | + MailMergeDataTable dataSource = new MailMergeDataTable("Employee", employeeList); |
| 29 | + //Performs Mail merge. |
| 30 | + document.MailMerge.ExecuteGroup(dataSource); |
| 31 | + //Creates file stream. |
| 32 | + using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 33 | + { |
| 34 | + //Saves the Word document to file stream. |
| 35 | + document.Save(outputStream, FormatType.Docx); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Remove a row if data is not defined in Data source |
| 43 | + /// </summary> |
| 44 | + private static void RemoveRowOfDataNotInDataSource(object sender, BeforeClearFieldEventArgs args) |
| 45 | + { |
| 46 | + if (args.GroupName == "Employee" && !args.HasMappedFieldInDataSource) |
| 47 | + { |
| 48 | + WParagraph paragraph = args.CurrentMergeField.OwnerParagraph; |
| 49 | + if (paragraph.IsInCell) |
| 50 | + { |
| 51 | + WTableRow tableRow = paragraph.Owner.Owner as WTableRow; |
| 52 | + (tableRow.Owner as WTable).Rows.Remove(tableRow); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Event handler to remove the row of empty or null value fields |
| 59 | + /// </summary> |
| 60 | + private static void RemoveRowsOfEmptyValue(object sender, MergeFieldEventArgs args) |
| 61 | + { |
| 62 | + if (args.GroupName == "Employee" && (args.FieldValue == DBNull.Value || args.FieldValue.ToString() == string.Empty)) |
| 63 | + { |
| 64 | + WParagraph paragraph = args.CurrentMergeField.OwnerParagraph; |
| 65 | + if (paragraph.IsInCell) |
| 66 | + { |
| 67 | + WTableRow tableRow = paragraph.Owner.Owner as WTableRow; |
| 68 | + (tableRow.Owner as WTable).Rows.Remove(tableRow); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Gets the employee details to perform mail merge. |
| 75 | + /// </summary> |
| 76 | + public static List<Employee> GetEmployees() |
| 77 | + { |
| 78 | + List<Employee> employees = new List<Employee> |
| 79 | + { |
| 80 | + new Employee("Nancy", "", "722 Moss Bay Blvd.", "USA"), |
| 81 | + new Employee("Andrew", "12/12/1988", "", "USA"), |
| 82 | + new Employee("Roland", "03/22/1992", "722 Moss Bay Blvd.", "USA"), |
| 83 | + new Employee("Margaret", "07/19/1980", "", "USA"), |
| 84 | + new Employee("Steven", "09/30/1995", "14 Garrett Hill", "") |
| 85 | + }; |
| 86 | + return employees; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Represents a class to maintain employee details. |
| 92 | + /// </summary> |
| 93 | + public class Employee |
| 94 | + { |
| 95 | + public string Name { get; set; } |
| 96 | + public string DOB { get; set; } |
| 97 | + public string Address { get; set; } |
| 98 | + public string Country { get; set; } |
| 99 | + |
| 100 | + public Employee(string name, string dob, string address, string country) |
| 101 | + { |
| 102 | + Name = name; |
| 103 | + DOB = dob; |
| 104 | + Address = address; |
| 105 | + Country = country; |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments