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