Skip to content

Commit 4bf47a2

Browse files
Merge pull request #488 from Kathiresan4347/main
992130-Add DocIO Sample for Remove Text Background color in Word Document
2 parents e5e292f + a8161b5 commit 4bf47a2

File tree

5 files changed

+192
-0
lines changed

5 files changed

+192
-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.14.36717.8 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Remove-background-color-to-bookmark-content", "Remove-background-color-to-bookmark-content\Remove-background-color-to-bookmark-content.csproj", "{9D5E50CE-B048-4A92-8EF2-B010F3C954A1}"
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+
{9D5E50CE-B048-4A92-8EF2-B010F3C954A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{9D5E50CE-B048-4A92-8EF2-B010F3C954A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{9D5E50CE-B048-4A92-8EF2-B010F3C954A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{9D5E50CE-B048-4A92-8EF2-B010F3C954A1}.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 = {877A6881-0522-42DB-8733-B66573CF76F2}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
namespace Remove_background_color_to_bookmark
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
// Create an input file stream to open the document
11+
using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read))
12+
{
13+
//Creates a new Word document.
14+
using (WordDocument document = new WordDocument(inputStream,FormatType.Docx))
15+
{
16+
// Create the bookmark navigator instance
17+
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
18+
// Move to the bookmark
19+
bookmarkNavigator.MoveToBookmark("Adventure_Bkmk");
20+
// Get the bookmark content
21+
TextBodyPart part = bookmarkNavigator.GetBookmarkContent();
22+
// Iterate through the content (implement this according to your needs)
23+
ModifyTextBodyPartFormat(part.BodyItems);
24+
// Replace the bookmark content with modified content
25+
bookmarkNavigator.ReplaceBookmarkContent(part);
26+
//Creates file stream.
27+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
28+
{
29+
//Saves the Word document to file stream.
30+
document.Save(outputFileStream, FormatType.Docx);
31+
}
32+
}
33+
}
34+
}
35+
/// <summary>
36+
/// Iterates through the body contents of the Word document.
37+
/// </summary>
38+
/// <param name="textBodyItems"></param>
39+
private static void ModifyTextBodyPartFormat(EntityCollection textBodyItems)
40+
{
41+
// Iterates through each of the child items of WTextBody
42+
for (int i = 0; i < textBodyItems.Count; i++)
43+
{
44+
// IEntity is the basic unit in DocIO DOM.
45+
// Accesses the body items (should be either paragraph, table or block content control) as IEntity
46+
IEntity bodyItemEntity = textBodyItems[i];
47+
48+
// A Text body has 3 types of elements - Paragraph, Table and Block Content Control
49+
// Decide the element type by using EntityType
50+
switch (bodyItemEntity.EntityType)
51+
{
52+
case EntityType.Paragraph:
53+
{
54+
WParagraph paragraph = bodyItemEntity as WParagraph;
55+
if (paragraph != null)
56+
{
57+
// Processes the paragraph contents
58+
// Iterates through the paragraph's DOM
59+
IterateParagraph(paragraph.Items);
60+
}
61+
break;
62+
}
63+
case EntityType.Table:
64+
{
65+
// Table is a collection of rows and cells
66+
// Iterates through table's DOM
67+
WTable table = bodyItemEntity as WTable;
68+
if (table != null)
69+
{
70+
IterateTable(table);
71+
}
72+
break;
73+
}
74+
case EntityType.BlockContentControl:
75+
{
76+
BlockContentControl blockContentControl = bodyItemEntity as BlockContentControl;
77+
if (blockContentControl != null && blockContentControl.TextBody != null)
78+
{
79+
// Iterates the body items of Block Content Control.
80+
ModifyTextBodyPartFormat(blockContentControl.TextBody.ChildEntities);
81+
}
82+
break;
83+
}
84+
}
85+
}
86+
}
87+
/// <summary>
88+
/// Iterates through the table.
89+
/// </summary>
90+
/// <param name="table"></param>
91+
private static void IterateTable(WTable table)
92+
{
93+
//Iterates the row collection in a table
94+
foreach (WTableRow row in table.Rows)
95+
{
96+
//Iterates the cell collection in a table row
97+
foreach (WTableCell cell in row.Cells)
98+
{
99+
//Table cell is derived from (also a) TextBody
100+
//Reusing the code meant for iterating TextBody
101+
ModifyTextBodyPartFormat(cell.ChildEntities);
102+
}
103+
}
104+
}
105+
/// <summary>
106+
/// Iterates through the paragraph.
107+
/// </summary>
108+
/// <param name="paraItems"></param>
109+
private static void IterateParagraph(ParagraphItemCollection paraItems)
110+
{
111+
for (int i = 0; i < paraItems.Count; i++)
112+
{
113+
Entity entity = paraItems[i];
114+
//A paragraph can have child elements such as text, image, hyperlink, symbols, etc.,
115+
//Decides the element type by using EntityType
116+
switch (entity.EntityType)
117+
{
118+
case EntityType.TextRange:
119+
//Remove the text back color
120+
WTextRange textRange = entity as WTextRange;
121+
textRange.CharacterFormat.TextBackgroundColor = Syncfusion.Drawing.Color.Empty;
122+
break;
123+
case EntityType.TextBox:
124+
//Iterates to the body items of textbox.
125+
WTextBox textBox = entity as WTextBox;
126+
ModifyTextBodyPartFormat(textBox.TextBoxBody.ChildEntities);
127+
break;
128+
case EntityType.Shape:
129+
//Iterates to the body items of shape.
130+
Shape shape = entity as Shape;
131+
ModifyTextBodyPartFormat(shape.TextBody.ChildEntities);
132+
break;
133+
case EntityType.InlineContentControl:
134+
//Iterates to the paragraph items of inline content control.
135+
InlineContentControl inlineContentControl = entity as InlineContentControl;
136+
IterateParagraph(inlineContentControl.ParagraphItems);
137+
break;
138+
}
139+
}
140+
}
141+
}
142+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Remove_background_color_to_bookmark_content</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\Template.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>

0 commit comments

Comments
 (0)