Skip to content

Commit bc614d6

Browse files
ES-957516-Apply-style-to-Bookmark-content
1 parent 410b6a3 commit bc614d6

File tree

6 files changed

+180
-0
lines changed

6 files changed

+180
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35527.113 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Apply-style-to-bookmark-content", "Apply-style-to-bookmark-content\Apply-style-to-bookmark-content.csproj", "{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}"
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+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
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>Apply_style_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>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
5+
// Declare a variable to hold the custom character style used for formatting bookmark content.
6+
WCharacterStyle style;
7+
8+
// Load the Word document.
9+
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
10+
{
11+
// Navigate to the bookmark named "Tiny_Cubes".
12+
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
13+
bookmarkNavigator.MoveToBookmark("Tiny_Cubes");
14+
15+
// Extract the content inside the bookmark as a separate Word document.
16+
WordDocument bookmarkContent = bookmarkNavigator.GetBookmarkContent().GetAsWordDocument();
17+
18+
// Retrieve the character style named "TinyCube" from the style collection.
19+
IStyleCollection styleCollection = document.Styles;
20+
style = styleCollection.FindByName("TinyCube") as WCharacterStyle;
21+
22+
// Apply the retrieved style to all elements in the extracted bookmark content.
23+
IterateDocumentElements(bookmarkContent);
24+
25+
// Create a WordDocumentPart from the modified bookmark content.
26+
WordDocumentPart wordDocumentPart = new WordDocumentPart(bookmarkContent);
27+
28+
// Replace the original bookmark content with the styled content.
29+
bookmarkNavigator.ReplaceContent(wordDocumentPart);
30+
31+
// Save the updated document to a new file.
32+
document.Save(Path.GetFullPath(@"Output/Result.docx"), FormatType.Docx);
33+
}
34+
35+
/// <summary>
36+
/// Iterates all sections, headers, and footers in the given document.
37+
/// </summary>
38+
void IterateDocumentElements(WordDocument document)
39+
{
40+
foreach (WSection section in document.Sections)
41+
{
42+
// Process the main body of the section.
43+
IterateTextBody(section.Body);
44+
45+
// Process the header and footer (only OddHeader and OddFooter here).
46+
WHeadersFooters headersFooters = section.HeadersFooters;
47+
IterateTextBody(headersFooters.OddHeader);
48+
IterateTextBody(headersFooters.OddFooter);
49+
}
50+
}
51+
52+
/// <summary>
53+
/// Iterates all entities (paragraphs, tables, block content controls) within a WTextBody.
54+
/// </summary>
55+
void IterateTextBody(WTextBody textBody)
56+
{
57+
for (int i = 0; i < textBody.ChildEntities.Count; i++)
58+
{
59+
IEntity bodyItemEntity = textBody.ChildEntities[i];
60+
61+
switch (bodyItemEntity.EntityType)
62+
{
63+
case EntityType.Paragraph:
64+
// Process paragraph items (text, fields, etc.)
65+
IterateParagraph((bodyItemEntity as WParagraph).Items);
66+
break;
67+
68+
case EntityType.Table:
69+
// Recursively process each cell in the table.
70+
IterateTable(bodyItemEntity as WTable);
71+
break;
72+
73+
case EntityType.BlockContentControl:
74+
// Recursively process the text body within a block content control.
75+
IterateTextBody((bodyItemEntity as BlockContentControl).TextBody);
76+
break;
77+
}
78+
}
79+
}
80+
81+
/// <summary>
82+
/// Iterates all rows and cells in a table, processing each cell's text body.
83+
/// </summary>
84+
void IterateTable(WTable table)
85+
{
86+
foreach (WTableRow row in table.Rows)
87+
{
88+
foreach (WTableCell cell in row.Cells)
89+
{
90+
// Each cell is a TextBody; reuse IterateTextBody to process its content.
91+
IterateTextBody(cell);
92+
}
93+
}
94+
}
95+
96+
/// <summary>
97+
/// Iterates all paragraph items and applies the specified style formatting.
98+
/// </summary>
99+
void IterateParagraph(ParagraphItemCollection paraItems)
100+
{
101+
for (int i = 0; i < paraItems.Count; i++)
102+
{
103+
Entity entity = paraItems[i];
104+
105+
switch (entity.EntityType)
106+
{
107+
case EntityType.TextRange:
108+
// Apply the character style to the text range.
109+
(entity as WTextRange).ApplyCharacterFormat(style.CharacterFormat);
110+
break;
111+
112+
case EntityType.Field:
113+
// Apply the character style to the field.
114+
(entity as WField).ApplyCharacterFormat(style.CharacterFormat);
115+
break;
116+
117+
case EntityType.TextBox:
118+
// Recursively process the contents of the textbox.
119+
IterateTextBody((entity as WTextBox).TextBoxBody);
120+
break;
121+
122+
case EntityType.Shape:
123+
// Recursively process the contents of the shape.
124+
IterateTextBody((entity as Shape).TextBody);
125+
break;
126+
127+
case EntityType.InlineContentControl:
128+
// Recursively process the paragraph items within the inline content control.
129+
IterateParagraph((entity as InlineContentControl).ParagraphItems);
130+
break;
131+
}
132+
}
133+
}

0 commit comments

Comments
 (0)