diff --git a/Mail-Merge/Apply-multiple-color-to-mergefield/.NET/Apply-multiple-color-to-mergefield.sln b/Mail-Merge/Apply-multiple-color-to-mergefield/.NET/Apply-multiple-color-to-mergefield.sln new file mode 100644 index 000000000..869580bc0 --- /dev/null +++ b/Mail-Merge/Apply-multiple-color-to-mergefield/.NET/Apply-multiple-color-to-mergefield.sln @@ -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}") = "Apply-multiple-color-to-mergefield", "Apply-multiple-color-to-mergefield\Apply-multiple-color-to-mergefield.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 diff --git a/Mail-Merge/Apply-multiple-color-to-mergefield/.NET/Apply-multiple-color-to-mergefield/Apply-multiple-color-to-mergefield.csproj b/Mail-Merge/Apply-multiple-color-to-mergefield/.NET/Apply-multiple-color-to-mergefield/Apply-multiple-color-to-mergefield.csproj new file mode 100644 index 000000000..a8e79398d --- /dev/null +++ b/Mail-Merge/Apply-multiple-color-to-mergefield/.NET/Apply-multiple-color-to-mergefield/Apply-multiple-color-to-mergefield.csproj @@ -0,0 +1,22 @@ + + + + Exe + net8.0 + Apply_multiple_color_to_mergefield + + + + + + + + + Always + + + Always + + + + diff --git a/Mail-Merge/Apply-multiple-color-to-mergefield/.NET/Apply-multiple-color-to-mergefield/Data/Template.docx b/Mail-Merge/Apply-multiple-color-to-mergefield/.NET/Apply-multiple-color-to-mergefield/Data/Template.docx new file mode 100644 index 000000000..9afbce432 Binary files /dev/null and b/Mail-Merge/Apply-multiple-color-to-mergefield/.NET/Apply-multiple-color-to-mergefield/Data/Template.docx differ diff --git a/Mail-Merge/Apply-multiple-color-to-mergefield/.NET/Apply-multiple-color-to-mergefield/Output/.gitkeep b/Mail-Merge/Apply-multiple-color-to-mergefield/.NET/Apply-multiple-color-to-mergefield/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Mail-Merge/Apply-multiple-color-to-mergefield/.NET/Apply-multiple-color-to-mergefield/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Mail-Merge/Apply-multiple-color-to-mergefield/.NET/Apply-multiple-color-to-mergefield/Program.cs b/Mail-Merge/Apply-multiple-color-to-mergefield/.NET/Apply-multiple-color-to-mergefield/Program.cs new file mode 100644 index 000000000..a23cf5d8b --- /dev/null +++ b/Mail-Merge/Apply-multiple-color-to-mergefield/.NET/Apply-multiple-color-to-mergefield/Program.cs @@ -0,0 +1,94 @@ +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; +using Syncfusion.Drawing; +using System.IO; + +namespace Apply_multiple_color_to_mergefield +{ + 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)) + { + string[] fieldNames = new string[] { "RedBlack", "RedBlackGreen" }; + string[] fieldValues = new string[] { "Red Black", "Red Black Green" }; + //Creates mail merge events handler to split the field value and applies the color + document.MailMerge.MergeField += new MergeFieldEventHandler(MergeFieldEvent); + //Performs the mail merge + document.MailMerge.Execute(fieldNames, fieldValues); + //Removes mail merge events handler + document.MailMerge.MergeField -= new MergeFieldEventHandler(MergeFieldEvent); + + //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); + } + } + } + } + + /// + /// Splits the field value and applies the color by using MergeFieldEventHandler. + /// + public static void MergeFieldEvent(object sender, MergeFieldEventArgs args) + { + if (args.FieldName == "RedBlack" || args.FieldName == "RedBlackGreen") + { + //Split the field result value based on space between the words. + string[] splitText = args.FieldValue.ToString().Split(' '); + + //Modifies the field result text as "Red" and applies the color red. + args.TextRange.Text = splitText[0]; + if (args.TextRange.Text == "Red") + args.TextRange.CharacterFormat.TextColor = Color.Red; + + //Gets the merge field owner paragraph. + WParagraph paragraph = args.CurrentMergeField.OwnerParagraph; + //Gets the index of merge field + int fieldIndex = paragraph.ChildEntities.IndexOf(args.CurrentMergeField); + //Gets the index next to the merge field. + int fieldNextIndex = GetFieldNextIndex(fieldIndex, paragraph); + + //Appends the remaining texts after the merge field and applies the color. + for (int i = 1; i < splitText.Length; i++) + { + //Initialize new text range. + WTextRange textRange = new WTextRange(paragraph.Document); + //Specifies the text. + textRange.Text = " " + splitText[i]; + //Applies the color based on the text + if (textRange.Text == " " + "Black") + textRange.CharacterFormat.TextColor = Color.Black; + else if (textRange.Text == " " + "Green") + textRange.CharacterFormat.TextColor = Color.Green; + + //Appends the text range after the merge field. + if (fieldNextIndex != -1 && fieldNextIndex < paragraph.ChildEntities.Count) + paragraph.ChildEntities.Insert(fieldNextIndex, textRange); + else + paragraph.ChildEntities.Add(textRange); + fieldNextIndex++; + } + } + } + /// + /// Returns the index next to the merge field. + /// + public static int GetFieldNextIndex(int fieldIndex, WParagraph paragraph) + { + for (int i = fieldIndex; i < paragraph.ChildEntities.Count; i++) + { + ParagraphItem item = paragraph.ChildEntities[i] as ParagraphItem; + if (item != null && item is WFieldMark && (item as WFieldMark).Type == FieldMarkType.FieldEnd) + return i + 1; + } + return -1; + } + } +}