Skip to content

Commit 37bbfd2

Browse files
committed
Added the sample
1 parent 859e6bf commit 37bbfd2

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-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.36518.9 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Find-Bookmark-Owner-TextBody-or-Header-Footer", "Find-Bookmark-Owner-TextBody-or-Header-Footer\Find-Bookmark-Owner-TextBody-or-Header-Footer.csproj", "{73FE0BC7-3E61-4093-864D-77BC36251331}"
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+
{73FE0BC7-3E61-4093-864D-77BC36251331}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{73FE0BC7-3E61-4093-864D-77BC36251331}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{73FE0BC7-3E61-4093-864D-77BC36251331}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{73FE0BC7-3E61-4093-864D-77BC36251331}.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 = {1FF6D9EE-60E6-40F2-94B4-AC0FD9D92EA3}
24+
EndGlobalSection
25+
EndGlobal
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_Bookmark_Owner_TextBody_or_Header_Footer</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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using Syncfusion.DocIO.DLS;
2+
3+
namespace Find_Bookmark_Owner_TextBody_or_Header_Footer
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 all bookmarks in the document.
12+
foreach (Bookmark bookmark in document.Bookmarks)
13+
{
14+
// Get bookmark start from the current bookmark
15+
BookmarkStart bkmkStart = bookmark.BookmarkStart;
16+
if (bkmkStart != null)
17+
{
18+
// Get the paragraphs that contain the bookmark's start.
19+
WParagraph startPara = bkmkStart.OwnerParagraph;
20+
if (startPara == null)
21+
continue;
22+
23+
Entity ownerEntity = startPara;
24+
if (ownerEntity != null)
25+
{
26+
// Traverse the owner hierarchy until reaching the section, stopping if a HeaderFooter is found
27+
while (!(ownerEntity is WSection))
28+
{
29+
if (ownerEntity.EntityType == EntityType.HeaderFooter)
30+
break;
31+
ownerEntity = ownerEntity.Owner;
32+
}
33+
}
34+
// Check if the bookmark is in the text body, header, or footer
35+
string ownerLabel = (ownerEntity.EntityType == EntityType.Section)
36+
? "TextBody"
37+
: CheckHeaderFooterType(ownerEntity.Owner as WSection, ownerEntity as HeaderFooter);
38+
// Print the bookmark name and its owner type
39+
Console.WriteLine("Bookmark Name:" + bkmkStart.Name + "\n Bookmark Owner:" + ownerLabel);
40+
}
41+
}
42+
Console.ReadLine();
43+
}
44+
/// <summary>
45+
/// Returns a whether the provided HeaderFooter instance belongs to the header or footer of the given section.
46+
/// </summary>
47+
/// <param name="section">The section that contains the HeaderFooter</param>
48+
/// <param name="headerFooter">The HeaderFooter instance to check.</param>
49+
/// <returns>Returns "Header" if the instance is a header, "Footer" if it is a footer,
50+
/// otherwise "Header and Footer".</returns>
51+
private static string CheckHeaderFooterType(WSection section, HeaderFooter headerFooter)
52+
{
53+
string type = "Header and Footer";
54+
// Check if the given HeaderFooter instance is one of the section's header references.
55+
if (section.HeadersFooters.OddHeader == headerFooter
56+
|| section.HeadersFooters.FirstPageHeader == headerFooter || section.HeadersFooters.EvenHeader == headerFooter)
57+
{
58+
type = "Header";
59+
}
60+
// Otherwise, check if it is one of the section's footer references.
61+
else if (section.HeadersFooters.OddFooter == headerFooter || section.HeadersFooters.EvenFooter == headerFooter
62+
|| section.HeadersFooters.FirstPageFooter == headerFooter)
63+
{
64+
type = "Footer";
65+
}
66+
return type;
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)