1+ using Syncfusion . DocIO . DLS ;
2+
3+ namespace Find_nested_bookmark_in_Word_document
4+ {
5+ class Program
6+ {
7+ public static void Main ( string [ ] args )
8+ {
9+ // Load the existing Word document.
10+ WordDocument document = new WordDocument ( Path . GetFullPath ( @"Data/Template.docx" ) ) ;
11+ //Iterate through the bookmark collection
12+ foreach ( Bookmark bookmark in document . Bookmarks )
13+ {
14+ // Create a collection to store nested bookmarks
15+ List < string > nestedBookmarks = new List < string > ( ) ;
16+ // Create a BookmarksNavigator for the current bookmark
17+ BookmarksNavigator bookmarksNavigator = new BookmarksNavigator ( document ) ;
18+ {
19+ // Move navigator to the current bookmark
20+ bookmarksNavigator . MoveToBookmark ( bookmark . Name ) ;
21+ // Retrieve the bookmark content as a WordDocument
22+ using ( WordDocument content = bookmarksNavigator . GetContent ( ) . GetAsWordDocument ( ) )
23+ {
24+ // Remove headers and footers from the extracted document
25+ RemoveHeaderFooter ( content ) ;
26+ // Iterate through bookmarks in the extracted document
27+ foreach ( Bookmark bm in content . Bookmarks )
28+ {
29+ // Exclude the parent itself if names are identical
30+ if ( ! bm . Name . Equals ( bookmark . Name ) )
31+ nestedBookmarks . Add ( bm . Name ) ;
32+ }
33+ }
34+ // Print the parent bookmark and its nested bookmarks
35+ Console . WriteLine ( "Parent Bookmark: " + bookmark . Name ) ;
36+ foreach ( string name in nestedBookmarks )
37+ Console . WriteLine ( "Nested Bookmark: " + name ) ;
38+ Console . WriteLine ( "************************" ) ;
39+ }
40+ }
41+ Console . ReadLine ( ) ;
42+ }
43+ /// <summary>
44+ /// Removes all headers and footers from every section in the specified Word document.
45+ /// </summary>
46+ /// <param name="doc">The WordDocument instance to process and clear headers and footers.</param>
47+ /// <remarks>
48+ /// This method iterates through each section of the document and clears
49+ /// the child entities of all header and footer types (first page, odd, even).
50+ /// After clearing, it adds an empty paragraph to each header and footer
51+ /// to preserve the document’s structure.
52+ /// </remarks>
53+ private static void RemoveHeaderFooter ( WordDocument doc )
54+ {
55+ //Iterate and Remove the Header / footer
56+ foreach ( WSection section in doc . Sections )
57+ {
58+ // Remove the first page header and footer
59+ section . HeadersFooters . FirstPageHeader . ChildEntities . Clear ( ) ;
60+ section . HeadersFooters . FirstPageHeader . AddParagraph ( ) ;
61+ section . HeadersFooters . FirstPageFooter . ChildEntities . Clear ( ) ;
62+ section . HeadersFooters . FirstPageFooter . AddParagraph ( ) ;
63+
64+ // Remove the odd page header and footer
65+ section . HeadersFooters . OddHeader . ChildEntities . Clear ( ) ;
66+ section . HeadersFooters . OddHeader . AddParagraph ( ) ;
67+ section . HeadersFooters . OddFooter . ChildEntities . Clear ( ) ;
68+ section . HeadersFooters . OddFooter . AddParagraph ( ) ;
69+
70+ // Remove the even page header and footer
71+ section . HeadersFooters . EvenHeader . ChildEntities . Clear ( ) ;
72+ section . HeadersFooters . EvenHeader . AddParagraph ( ) ;
73+ section . HeadersFooters . EvenFooter . ChildEntities . Clear ( ) ;
74+ section . HeadersFooters . EvenFooter . AddParagraph ( ) ;
75+ }
76+ }
77+ }
78+ }
0 commit comments