Skip to content

Commit 235d7a9

Browse files
Added sample for Change the text color of existing styles in a word document
1 parent 99a31d7 commit 235d7a9

File tree

5 files changed

+100
-0
lines changed

5 files changed

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

0 commit comments

Comments
 (0)