|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | + |
| 7 | +namespace Execute_GroupMailMerge_FirstOccurrenceOnly |
| 8 | +{ |
| 9 | + class Program |
| 10 | + { |
| 11 | + static void Main() |
| 12 | + { |
| 13 | + // Load the Word template document |
| 14 | + WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")); |
| 15 | + // Get all merge field names from the document |
| 16 | + string[] mergeGroupNames = document.MailMerge.GetMergeFieldNames(); |
| 17 | + // Create a dictionary to count how many times each merge field appears |
| 18 | + Dictionary<string, int> groupNameCounts = new Dictionary<string, int>(); |
| 19 | + foreach (string groupName in mergeGroupNames) |
| 20 | + { |
| 21 | + // Increase count if field already exists, else add it |
| 22 | + if (groupNameCounts.ContainsKey(groupName)) |
| 23 | + groupNameCounts[groupName]++; |
| 24 | + else |
| 25 | + groupNameCounts[groupName] = 1; |
| 26 | + } |
| 27 | + // Remove duplicate merge field groups |
| 28 | + foreach (var groupEntry in groupNameCounts) |
| 29 | + { |
| 30 | + string groupName = groupEntry.Key; |
| 31 | + // Skip if the field appears only once |
| 32 | + if (groupEntry.Value <= 1) |
| 33 | + continue; |
| 34 | + // Find all merge fields with the same name |
| 35 | + List<Entity> mergeGroups = document.FindAllItemsByProperty(EntityType.MergeField, "FieldName", groupName); |
| 36 | + // Start from second occurrence to remove duplicates |
| 37 | + for (int i = 1; i < mergeGroups.Count; i++) |
| 38 | + { |
| 39 | + WMergeField mergeField = mergeGroups[i] as WMergeField; |
| 40 | + // Check if it's a group start field |
| 41 | + if (mergeField.FieldCode.Contains("TableStart") || mergeField.FieldCode.Contains("BeginGroup")) |
| 42 | + { |
| 43 | + // Add bookmark start before the group |
| 44 | + BookmarkStart bkmkStart = new BookmarkStart(document, groupName); |
| 45 | + WParagraph startPara = mergeField.OwnerParagraph; |
| 46 | + int mergeFieldIndex = startPara.ChildEntities.IndexOf(mergeField); |
| 47 | + startPara.ChildEntities.Insert(mergeFieldIndex, bkmkStart); |
| 48 | + // Add bookmark end after the group |
| 49 | + WMergeField endField = mergeGroups[i + 1] as WMergeField; |
| 50 | + BookmarkEnd bkmkEnd = new BookmarkEnd(document, groupName); |
| 51 | + WParagraph endPara = endField.OwnerParagraph; |
| 52 | + int endFieldIndex = endPara.ChildEntities.IndexOf(endField); |
| 53 | + endPara.ChildEntities.Insert(endFieldIndex + 1, bkmkEnd); |
| 54 | + // Delete content inside the bookmark |
| 55 | + BookmarksNavigator navigator = new BookmarksNavigator(document); |
| 56 | + navigator.MoveToBookmark(groupName); |
| 57 | + navigator.DeleteBookmarkContent(false); |
| 58 | + document.Bookmarks.Remove(navigator.CurrentBookmark); |
| 59 | + // Remove owner table if applicable |
| 60 | + if (startPara.OwnerTextBody.Owner.EntityType == EntityType.TableRow) |
| 61 | + { |
| 62 | + WTableRow tableRow = startPara.OwnerTextBody.Owner as WTableRow; |
| 63 | + WTable ownerTable = tableRow.Owner as WTable; |
| 64 | + Entity currentEntity = ownerTable; |
| 65 | + // Traverse up to find the section and remove the table |
| 66 | + while (currentEntity != null) |
| 67 | + { |
| 68 | + if (currentEntity is WSection section) |
| 69 | + { |
| 70 | + section.Body.ChildEntities.Remove(ownerTable); |
| 71 | + break; |
| 72 | + } |
| 73 | + currentEntity = currentEntity.Owner; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + // Prepare nested mail merge data |
| 80 | + List<Organization> organizationList = GetOrganizations(); |
| 81 | + MailMergeDataTable dataTable = new MailMergeDataTable("Organizations", organizationList); |
| 82 | + // Execute nested mail merge using the data |
| 83 | + document.MailMerge.ExecuteNestedGroup(dataTable); |
| 84 | + // Save the result |
| 85 | + document.Save(Path.GetFullPath(@"../../../Output/Result.docx")); |
| 86 | + // Close the document |
| 87 | + document.Close(); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Create sample organization and employee data |
| 92 | + /// </summary> |
| 93 | + /// <returns>Return the organization list</returns> |
| 94 | + public static List<Organization> GetOrganizations() |
| 95 | + { |
| 96 | + // Create a list of employees |
| 97 | + List<EmployeeDetails> employees = new List<EmployeeDetails> |
| 98 | + { |
| 99 | + new EmployeeDetails("Thomas Hardy", "1001", "05/27/1996"), |
| 100 | + new EmployeeDetails("Maria Anders", "1002", "04/10/1998") |
| 101 | + }; |
| 102 | + // Create a list of organizations with employee data |
| 103 | + List<Organization> organizations = new List<Organization> |
| 104 | + { |
| 105 | + new Organization("UK Office", "120 Hanover Sq.", "UK", employees) |
| 106 | + }; |
| 107 | + // Return the organization list |
| 108 | + return organizations; |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments