|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | +using System.IO; |
| 4 | + |
| 5 | +namespace Modify_hindi_font_during_mail_merge |
| 6 | +{ |
| 7 | + class Program |
| 8 | + { |
| 9 | + static void Main(string[] args) |
| 10 | + { |
| 11 | + using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite)) |
| 12 | + { |
| 13 | + //Loads an existing Word document into DocIO instance. |
| 14 | + using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic)) |
| 15 | + { |
| 16 | + string[] fieldNames = new string[] { "EmployeeId", "Name", "Phone", "City" }; |
| 17 | + string[] fieldValues = new string[] { "1001", "नैन्सी डेवियलो", "+122-2222222", "London" }; |
| 18 | + |
| 19 | + // Uses the mail merge events to perform the conditional formatting during runtime. |
| 20 | + document.MailMerge.MergeField += new MergeFieldEventHandler(ApplyFontForHindiText); |
| 21 | + //Performs the mail merge. |
| 22 | + document.MailMerge.Execute(fieldNames, fieldValues); |
| 23 | + |
| 24 | + //Creates file stream. |
| 25 | + using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 26 | + { |
| 27 | + //Saves the Word document to file stream. |
| 28 | + document.Save(outputStream, FormatType.Docx); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Represents the method that handles the MergeField event. |
| 36 | + /// </summary> |
| 37 | + private static void ApplyFontForHindiText(object sender, MergeFieldEventArgs args) |
| 38 | + { |
| 39 | + string fieldValue = args.FieldValue.ToString(); |
| 40 | + //If the field value contains Hindi characters, then apply the font. |
| 41 | + bool containsHindi = ContainsHindiCharacters(fieldValue); |
| 42 | + if (containsHindi) |
| 43 | + { |
| 44 | + args.TextRange.CharacterFormat.FontName = "Nirmala UI"; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Checks whether the given character is a Hindi character or not. |
| 50 | + /// </summary> |
| 51 | + private static bool IsHindiChar(char character) |
| 52 | + { |
| 53 | + //Hindi characters are comes under the Devanagari scripts. |
| 54 | + //The Unicode Standard defines three blocks for Devanagari. https://en.wikipedia.org/wiki/Devanagari#Unicode |
| 55 | + return ((character >= '\u0900' && character <= '\u097f') //Devanagari (U+0900–U+097F), https://en.wikipedia.org/wiki/Devanagari_(Unicode_block) |
| 56 | + || (character >= '\ua8e0' && character <= '\ua8ff') //Devanagari Extended (U+A8E0–U+A8FF), https://en.wikipedia.org/wiki/Devanagari_Extended |
| 57 | + || (character >= '\u1cd0' && character <= '\u1cff')); //Vedic Extensions (U+1CD0–U+1CFF), https://en.wikipedia.org/wiki/Vedic_Extensions |
| 58 | + } |
| 59 | + /// <summary> |
| 60 | + /// Checks whether the given text contains Hindi characters or not. |
| 61 | + /// </summary> |
| 62 | + private static bool ContainsHindiCharacters(string text) |
| 63 | + { |
| 64 | + foreach (char character in text) |
| 65 | + { |
| 66 | + if (IsHindiChar(character)) |
| 67 | + { |
| 68 | + return true; // If any Hindi character is found, return true. |
| 69 | + } |
| 70 | + } |
| 71 | + return false; // No Hindi characters were found. |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments