Skip to content

Commit 02b913b

Browse files
ES-849725-Modify-hindi-font-during-mail-merge
1 parent 102d5f1 commit 02b913b

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31911.196
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
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}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {58137FF9-5AE1-4514-9929-3A8A7DA1DFEB}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Modify_hindi_font_during_mail_merge</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\Template.docx">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Output\.gitkeep">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)