|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | +using Syncfusion.Drawing; |
| 4 | +using System.IO; |
| 5 | + |
| 6 | +namespace Apply_multiple_color_to_mergefield |
| 7 | +{ |
| 8 | + class Program |
| 9 | + { |
| 10 | + static void Main(string[] args) |
| 11 | + { |
| 12 | + using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite)) |
| 13 | + { |
| 14 | + //Loads an existing Word document into DocIO instance. |
| 15 | + using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic)) |
| 16 | + { |
| 17 | + string[] fieldNames = new string[] { "RedBlack", "RedBlackGreen" }; |
| 18 | + string[] fieldValues = new string[] { "Red Black", "Red Black Green" }; |
| 19 | + //Creates mail merge events handler to split the field value and applies the color |
| 20 | + document.MailMerge.MergeField += new MergeFieldEventHandler(MergeFieldEvent); |
| 21 | + //Performs the mail merge |
| 22 | + document.MailMerge.Execute(fieldNames, fieldValues); |
| 23 | + //Removes mail merge events handler |
| 24 | + document.MailMerge.MergeField -= new MergeFieldEventHandler(MergeFieldEvent); |
| 25 | + |
| 26 | + //Creates file stream. |
| 27 | + using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 28 | + { |
| 29 | + //Saves the Word document to file stream. |
| 30 | + document.Save(outputStream, FormatType.Docx); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Splits the field value and applies the color by using MergeFieldEventHandler. |
| 38 | + /// </summary> |
| 39 | + public static void MergeFieldEvent(object sender, MergeFieldEventArgs args) |
| 40 | + { |
| 41 | + if (args.FieldName == "RedBlack" || args.FieldName == "RedBlackGreen") |
| 42 | + { |
| 43 | + //Split the field result value based on space between the words. |
| 44 | + string[] splitText = args.FieldValue.ToString().Split(' '); |
| 45 | + |
| 46 | + //Modifies the field result text as "Red" and applies the color red. |
| 47 | + args.TextRange.Text = splitText[0]; |
| 48 | + if (args.TextRange.Text == "Red") |
| 49 | + args.TextRange.CharacterFormat.TextColor = Color.Red; |
| 50 | + |
| 51 | + //Gets the merge field owner paragraph. |
| 52 | + WParagraph paragraph = args.CurrentMergeField.OwnerParagraph; |
| 53 | + //Gets the index of merge field |
| 54 | + int fieldIndex = paragraph.ChildEntities.IndexOf(args.CurrentMergeField); |
| 55 | + //Gets the index next to the merge field. |
| 56 | + int fieldNextIndex = GetFieldNextIndex(fieldIndex, paragraph); |
| 57 | + |
| 58 | + //Appends the remaining texts after the merge field and applies the color. |
| 59 | + for (int i = 1; i < splitText.Length; i++) |
| 60 | + { |
| 61 | + //Initialize new text range. |
| 62 | + WTextRange textRange = new WTextRange(paragraph.Document); |
| 63 | + //Specifies the text. |
| 64 | + textRange.Text = " " + splitText[i]; |
| 65 | + //Applies the color based on the text |
| 66 | + if (textRange.Text == " " + "Black") |
| 67 | + textRange.CharacterFormat.TextColor = Color.Black; |
| 68 | + else if (textRange.Text == " " + "Green") |
| 69 | + textRange.CharacterFormat.TextColor = Color.Green; |
| 70 | + |
| 71 | + //Appends the text range after the merge field. |
| 72 | + if (fieldNextIndex != -1 && fieldNextIndex < paragraph.ChildEntities.Count) |
| 73 | + paragraph.ChildEntities.Insert(fieldNextIndex, textRange); |
| 74 | + else |
| 75 | + paragraph.ChildEntities.Add(textRange); |
| 76 | + fieldNextIndex++; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + /// <summary> |
| 81 | + /// Returns the index next to the merge field. |
| 82 | + /// </summary> |
| 83 | + public static int GetFieldNextIndex(int fieldIndex, WParagraph paragraph) |
| 84 | + { |
| 85 | + for (int i = fieldIndex; i < paragraph.ChildEntities.Count; i++) |
| 86 | + { |
| 87 | + ParagraphItem item = paragraph.ChildEntities[i] as ParagraphItem; |
| 88 | + if (item != null && item is WFieldMark && (item as WFieldMark).Type == FieldMarkType.FieldEnd) |
| 89 | + return i + 1; |
| 90 | + } |
| 91 | + return -1; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments