diff --git a/Mail-Merge/Modify-hindi-font-during-mail-merge/.NET/Modify-hindi-font-during-mail-merge.sln b/Mail-Merge/Modify-hindi-font-during-mail-merge/.NET/Modify-hindi-font-during-mail-merge.sln new file mode 100644 index 000000000..326866e82 --- /dev/null +++ b/Mail-Merge/Modify-hindi-font-during-mail-merge/.NET/Modify-hindi-font-during-mail-merge.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}") = "Modify-hindi-font-during-mail-merge", "Modify-hindi-font-during-mail-merge\Modify-hindi-font-during-mail-merge.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/Modify-hindi-font-during-mail-merge/.NET/Modify-hindi-font-during-mail-merge/Data/Template.docx b/Mail-Merge/Modify-hindi-font-during-mail-merge/.NET/Modify-hindi-font-during-mail-merge/Data/Template.docx new file mode 100644 index 000000000..233d5d421 Binary files /dev/null and b/Mail-Merge/Modify-hindi-font-during-mail-merge/.NET/Modify-hindi-font-during-mail-merge/Data/Template.docx differ diff --git a/Mail-Merge/Modify-hindi-font-during-mail-merge/.NET/Modify-hindi-font-during-mail-merge/Modify-hindi-font-during-mail-merge.csproj b/Mail-Merge/Modify-hindi-font-during-mail-merge/.NET/Modify-hindi-font-during-mail-merge/Modify-hindi-font-during-mail-merge.csproj new file mode 100644 index 000000000..c59799d73 --- /dev/null +++ b/Mail-Merge/Modify-hindi-font-during-mail-merge/.NET/Modify-hindi-font-during-mail-merge/Modify-hindi-font-during-mail-merge.csproj @@ -0,0 +1,22 @@ + + + + Exe + net8.0 + Modify_hindi_font_during_mail_merge + + + + + + + + + Always + + + Always + + + + diff --git a/Mail-Merge/Modify-hindi-font-during-mail-merge/.NET/Modify-hindi-font-during-mail-merge/Output/.gitkeep b/Mail-Merge/Modify-hindi-font-during-mail-merge/.NET/Modify-hindi-font-during-mail-merge/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Mail-Merge/Modify-hindi-font-during-mail-merge/.NET/Modify-hindi-font-during-mail-merge/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Mail-Merge/Modify-hindi-font-during-mail-merge/.NET/Modify-hindi-font-during-mail-merge/Program.cs b/Mail-Merge/Modify-hindi-font-during-mail-merge/.NET/Modify-hindi-font-during-mail-merge/Program.cs new file mode 100644 index 000000000..969fc0562 --- /dev/null +++ b/Mail-Merge/Modify-hindi-font-during-mail-merge/.NET/Modify-hindi-font-during-mail-merge/Program.cs @@ -0,0 +1,74 @@ +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; +using System.IO; + +namespace Modify_hindi_font_during_mail_merge +{ + 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[] { "EmployeeId", "Name", "Phone", "City" }; + string[] fieldValues = new string[] { "1001", "नैन्सी डेवियलो", "+122-2222222", "London" }; + + // Uses the mail merge events to perform the conditional formatting during runtime. + document.MailMerge.MergeField += new MergeFieldEventHandler(ApplyFontForHindiText); + //Performs the mail merge. + document.MailMerge.Execute(fieldNames, fieldValues); + + //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); + } + } + } + } + + /// + /// Represents the method that handles the MergeField event. + /// + private static void ApplyFontForHindiText(object sender, MergeFieldEventArgs args) + { + string fieldValue = args.FieldValue.ToString(); + //If the field value contains Hindi characters, then apply the font. + bool containsHindi = ContainsHindiCharacters(fieldValue); + if (containsHindi) + { + args.TextRange.CharacterFormat.FontName = "Nirmala UI"; + } + } + + /// + /// Checks whether the given character is a Hindi character or not. + /// + private static bool IsHindiChar(char character) + { + //Hindi characters are comes under the Devanagari scripts. + //The Unicode Standard defines three blocks for Devanagari. https://en.wikipedia.org/wiki/Devanagari#Unicode + return ((character >= '\u0900' && character <= '\u097f') //Devanagari (U+0900–U+097F), https://en.wikipedia.org/wiki/Devanagari_(Unicode_block) + || (character >= '\ua8e0' && character <= '\ua8ff') //Devanagari Extended (U+A8E0–U+A8FF), https://en.wikipedia.org/wiki/Devanagari_Extended + || (character >= '\u1cd0' && character <= '\u1cff')); //Vedic Extensions (U+1CD0–U+1CFF), https://en.wikipedia.org/wiki/Vedic_Extensions + } + /// + /// Checks whether the given text contains Hindi characters or not. + /// + private static bool ContainsHindiCharacters(string text) + { + foreach (char character in text) + { + if (IsHindiChar(character)) + { + return true; // If any Hindi character is found, return true. + } + } + return false; // No Hindi characters were found. + } + } +}