Skip to content

Commit cfad4eb

Browse files
committed
Added the sample
1 parent 7c29bb8 commit cfad4eb

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="Find-nested-bookmark-in-Word-document/Find-nested-bookmark-in-Word-document.csproj" />
3+
</Solution>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Find_nested_bookmark_in_Word_document</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+
</ItemGroup>
20+
21+
</Project>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)