diff --git a/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge.sln b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge.sln new file mode 100644 index 000000000..b3b9958bf --- /dev/null +++ b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36518.9 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Insert-hyperlink-during-Mailmerge", "Insert-hyperlink-during-Mailmerge\Insert-hyperlink-during-Mailmerge.csproj", "{DAE76AF9-49AD-4C2E-8064-430DFD8B7726}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DAE76AF9-49AD-4C2E-8064-430DFD8B7726}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DAE76AF9-49AD-4C2E-8064-430DFD8B7726}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DAE76AF9-49AD-4C2E-8064-430DFD8B7726}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DAE76AF9-49AD-4C2E-8064-430DFD8B7726}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {344A442C-CA70-4696-8016-6BA6E07458A3} + EndGlobalSection +EndGlobal diff --git a/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Data/Andrew.png b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Data/Andrew.png new file mode 100644 index 000000000..c038c884a Binary files /dev/null and b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Data/Andrew.png differ diff --git a/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Data/EmployeesTemplate.docx b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Data/EmployeesTemplate.docx new file mode 100644 index 000000000..1661f0933 Binary files /dev/null and b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Data/EmployeesTemplate.docx differ diff --git a/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Data/Janet.png b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Data/Janet.png new file mode 100644 index 000000000..c86c4e53d Binary files /dev/null and b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Data/Janet.png differ diff --git a/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Data/Margaret.png b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Data/Margaret.png new file mode 100644 index 000000000..861f5907a Binary files /dev/null and b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Data/Margaret.png differ diff --git a/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Data/Nancy.png b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Data/Nancy.png new file mode 100644 index 000000000..677201146 Binary files /dev/null and b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Data/Nancy.png differ diff --git a/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Data/Steven.png b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Data/Steven.png new file mode 100644 index 000000000..dba7673f8 Binary files /dev/null and b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Data/Steven.png differ diff --git a/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Insert-hyperlink-during-Mailmerge.csproj b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Insert-hyperlink-during-Mailmerge.csproj new file mode 100644 index 000000000..4530e909b --- /dev/null +++ b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Insert-hyperlink-during-Mailmerge.csproj @@ -0,0 +1,39 @@ + + + + Exe + net8.0 + Insert_hyperlink_during_Mailmerge + enable + enable + + + + + + + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + + diff --git a/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Output/.gitkeep b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Program.cs b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Program.cs new file mode 100644 index 000000000..9b698b34e --- /dev/null +++ b/Mail-Merge/Insert-hyperlink-during-Mailmerge/.NET/Insert-hyperlink-during-Mailmerge/Program.cs @@ -0,0 +1,144 @@ +using Syncfusion.DocIO.DLS; + +namespace Insert_hyperlink_during_mailmerge +{ + class Program + { + static Dictionary> paraToInsertHyperlink = new Dictionary>(); + public static void Main(string[] args) + { + // Open the template Word document + WordDocument document = new WordDocument(Path.GetFullPath(@"Data/EmployeesTemplate.docx")); + // Gets the employee details as IEnumerable collection. + List 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); + // Uses the mail merge event handler for merge fields. + document.MailMerge.MergeField += MailMerge_MergeField; + // Performs Mail merge. + document.MailMerge.ExecuteGroup(dataSource); + // Insert Hyperlink to the merge field text. + InsertHyperlink(document); + // Save the result document + document.Save(Path.GetFullPath(@"../../../Output/output.docx")); + // Close the Word document + document.Close(); + } + /// + /// Event handler that customizes how merge fields are processed during mail merge. + /// + /// The source object raising the event (MailMerge engine). + /// Provides details about the current merge field being processed + private static void MailMerge_MergeField(object sender, MergeFieldEventArgs args) + { + + // Check if the current merge field is "Contact" + if (args.FieldName == "Contact") + { + // Get the mergefield's Owner paragraph + WParagraph mergeFieldOwnerParagraph = args.CurrentMergeField.OwnerParagraph; + // Check if this paragraph already has an entry in the dictionary. + // If not, create a new list to store field index. + if (!paraToInsertHyperlink.TryGetValue(mergeFieldOwnerParagraph, out var fields)) + { + fields = new List(); + paraToInsertHyperlink[mergeFieldOwnerParagraph] = fields; + } + // Add the current merge field's index + fields.Add(mergeFieldOwnerParagraph.ChildEntities.IndexOf(args.CurrentMergeField)); + } + } + /// + /// Inserts hyperlinks into the Word document at the positions of merge fields + /// + /// The WordDocument object being processed. + private static void InsertHyperlink(WordDocument document) + { + foreach (KeyValuePair> dictionaryItems in paraToInsertHyperlink) + { + // Get the paragraph where Hyperlink needs to be inserted. + WParagraph paragraph = dictionaryItems.Key; + // Get the list of index for the merge fields. + List values = dictionaryItems.Value; + // Iterate through the list in reverse order + for (int i = values.Count - 1; i >= 0; i--) + { + // Get the index of the merge field within the paragraph. + int index = values[i]; + // Get the merge field content to insert as Hyperlink. + WTextRange mergeFieldText = paragraph.ChildEntities[index] as WTextRange; + if (mergeFieldText != null) + { + string hyperlinkText = mergeFieldText.Text; + WParagraph hyperlinkParagraph = new WParagraph(document); + WField hyperlink = hyperlinkParagraph.AppendHyperlink(hyperlinkText, hyperlinkText, HyperlinkType.WebLink) as WField; + // Insert the child entity (e.g., hyperlink) from the new paragraph into the original paragraph + for (int j = hyperlinkParagraph.ChildEntities.Count - 1; j >= 0; j--) + { + paragraph.ChildEntities.Insert(index, hyperlinkParagraph.ChildEntities[j].Clone()); + } + // Remove the original merge field text from the paragraph + paragraph.ChildEntities.Remove(mergeFieldText); + } + } + } + paraToInsertHyperlink.Clear(); + } + /// + /// Represents the method that handles MergeImageField event. + /// + 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 employee details to perform mail merge. + /// + public static List GetEmployees() + { + List employees = new List(); + employees.Add(new Employee("Nancy", "Smith", "Sales Representative", "505 - 20th Ave. E. Apt. 2A,", "Seattle", "WA", "USA","nancy.smith@xyz.com", "Nancy.png")); + employees.Add(new Employee("Andrew", "Fuller", "Vice President, Sales", "908 W. Capital Way", "Tacoma", "WA", "USA", "andrew.fuller@xyz.com", "Andrew.png")); + employees.Add(new Employee("Roland", "Mendel", "Sales Representative", "722 Moss Bay Blvd.", "Kirkland", "WA", "USA", "roland.mendel@xyz.com", "Janet.png")); + employees.Add(new Employee("Margaret", "Peacock", "Sales Representative", "4110 Old Redmond Rd.", "Redmond", "WA", "USA", "margaret.peacock@xyz.com", "Margaret.png")); + employees.Add(new Employee("Steven", "Buchanan", "Sales Manager", "14 Garrett Hill", "London", string.Empty, "UK", "steven.buchanan@xyz.com", "Steven.png")); + return employees; + } + /// + /// Represents a class to maintain employee details. + /// + 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 Contact { get; set; } + public string Photo { get; set; } + public Employee(string firstName, string lastName, string title, string address, string city, string region, string country, string contact, string photoFilePath) + { + FirstName = firstName; + LastName = lastName; + Title = title; + Address = address; + City = city; + Region = region; + Country = country; + Contact = contact; + Photo = photoFilePath; + } + } + } +}