Skip to content
Merged
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}") = "Apply-multiple-color-to-mergefield", "Apply-multiple-color-to-mergefield\Apply-multiple-color-to-mergefield.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
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>Apply_multiple_color_to_mergefield</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>
Binary file not shown.
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,94 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.Drawing;
using System.IO;

namespace Apply_multiple_color_to_mergefield
{
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[] { "RedBlack", "RedBlackGreen" };
string[] fieldValues = new string[] { "Red Black", "Red Black Green" };
//Creates mail merge events handler to split the field value and applies the color
document.MailMerge.MergeField += new MergeFieldEventHandler(MergeFieldEvent);
//Performs the mail merge
document.MailMerge.Execute(fieldNames, fieldValues);
//Removes mail merge events handler
document.MailMerge.MergeField -= new MergeFieldEventHandler(MergeFieldEvent);

//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>
/// Splits the field value and applies the color by using MergeFieldEventHandler.
/// </summary>
public static void MergeFieldEvent(object sender, MergeFieldEventArgs args)
{
if (args.FieldName == "RedBlack" || args.FieldName == "RedBlackGreen")
{
//Split the field result value based on space between the words.
string[] splitText = args.FieldValue.ToString().Split(' ');

//Modifies the field result text as "Red" and applies the color red.
args.TextRange.Text = splitText[0];
if (args.TextRange.Text == "Red")
args.TextRange.CharacterFormat.TextColor = Color.Red;

//Gets the merge field owner paragraph.
WParagraph paragraph = args.CurrentMergeField.OwnerParagraph;
//Gets the index of merge field
int fieldIndex = paragraph.ChildEntities.IndexOf(args.CurrentMergeField);
//Gets the index next to the merge field.
int fieldNextIndex = GetFieldNextIndex(fieldIndex, paragraph);

//Appends the remaining texts after the merge field and applies the color.
for (int i = 1; i < splitText.Length; i++)
{
//Initialize new text range.
WTextRange textRange = new WTextRange(paragraph.Document);
//Specifies the text.
textRange.Text = " " + splitText[i];
//Applies the color based on the text
if (textRange.Text == " " + "Black")
textRange.CharacterFormat.TextColor = Color.Black;
else if (textRange.Text == " " + "Green")
textRange.CharacterFormat.TextColor = Color.Green;

//Appends the text range after the merge field.
if (fieldNextIndex != -1 && fieldNextIndex < paragraph.ChildEntities.Count)
paragraph.ChildEntities.Insert(fieldNextIndex, textRange);
else
paragraph.ChildEntities.Add(textRange);
fieldNextIndex++;
}
}
}
/// <summary>
/// Returns the index next to the merge field.
/// </summary>
public static int GetFieldNextIndex(int fieldIndex, WParagraph paragraph)
{
for (int i = fieldIndex; i < paragraph.ChildEntities.Count; i++)
{
ParagraphItem item = paragraph.ChildEntities[i] as ParagraphItem;
if (item != null && item is WFieldMark && (item as WFieldMark).Type == FieldMarkType.FieldEnd)
return i + 1;
}
return -1;
}
}
}
Loading