Skip to content

Commit d688339

Browse files
Merge pull request #389 from SyncfusionExamples/ES-263540-Apply-multiple-color-to-mergefield
ES-263540- Add the sample Apply-multiple-color-to-mergefield
2 parents c05c897 + aee0d87 commit d688339

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-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}") = "Apply-multiple-color-to-mergefield", "Apply-multiple-color-to-mergefield\Apply-multiple-color-to-mergefield.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>Apply_multiple_color_to_mergefield</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: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)