Skip to content

ES-849725- Add the sample Modify-hindi-font-during-mail-merge #392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Modify_hindi_font_during_mail_merge</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}

/// <summary>
/// Represents the method that handles the MergeField event.
/// </summary>
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";
}
}

/// <summary>
/// Checks whether the given character is a Hindi character or not.
/// </summary>
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
}
/// <summary>
/// Checks whether the given text contains Hindi characters or not.
/// </summary>
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.
}
}
}
Loading