Skip to content

Commit 146e2bf

Browse files
Merge pull request #273 from VijayadharshiniMathiyalagan/ES-881088-How-to-change-the-text-color-of-existing-styles-in-a-Word-document
Add sample for Change the text color of existing styles in a Word document
2 parents 6bd2098 + 3e89c17 commit 146e2bf

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-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 17
4+
VisualStudioVersion = 17.12.35309.182
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Change-text-color-of-styles", "Change-text-color-of-styles\Change-text-color-of-styles.csproj", "{F727F5FB-8626-4542-A59D-703BD3937FA3}"
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+
{F727F5FB-8626-4542-A59D-703BD3937FA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{F727F5FB-8626-4542-A59D-703BD3937FA3}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{F727F5FB-8626-4542-A59D-703BD3937FA3}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{F727F5FB-8626-4542-A59D-703BD3937FA3}.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 = {AEF532D2-87BC-4F0A-A194-F26F62F1CA5B}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Change_text_color_of_styles</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Input.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Data\Template.docx">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
<None Update="Output\.gitkeep">
23+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
24+
</None>
25+
</ItemGroup>
26+
27+
</Project>
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.DocIO;
3+
using Syncfusion.Drawing;
4+
5+
using (FileStream inputStream = new FileStream(Path.GetFullPath("Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
6+
{
7+
// Open the input Word document.
8+
using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
9+
{
10+
// Iterate through all the styles in the document
11+
foreach (Style style in document.Styles)
12+
{
13+
// Check if the style is a Paragraph style.
14+
if (style.StyleType == StyleType.ParagraphStyle)
15+
{
16+
// Cast the style to WParagraphStyle and modify the text color.
17+
WParagraphStyle paraStyle = style as WParagraphStyle;
18+
if (paraStyle != null)
19+
paraStyle.CharacterFormat.TextColor = Color.Purple;
20+
}
21+
// Check if the style is a Character style.
22+
else if (style.StyleType == StyleType.CharacterStyle)
23+
{
24+
// Cast the style to WCharacterStyle and modify the text color.
25+
WCharacterStyle charStyle = style as WCharacterStyle;
26+
if (charStyle != null)
27+
charStyle.CharacterFormat.TextColor = Color.Red;
28+
}
29+
}
30+
// Save the modified document.
31+
using (FileStream outputStream = new FileStream(Path.GetFullPath("Output/Result.docx"), FileMode.Create, FileAccess.Write))
32+
{
33+
document.Save(outputStream, FormatType.Docx);
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)