Skip to content

Commit d4e2f77

Browse files
Changes added
1 parent e5e292f commit d4e2f77

File tree

5 files changed

+197
-0
lines changed

5 files changed

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