Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36518.9 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{73FE0BC7-3E61-4093-864D-77BC36251331}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{73FE0BC7-3E61-4093-864D-77BC36251331}.Debug|Any CPU.Build.0 = Debug|Any CPU
{73FE0BC7-3E61-4093-864D-77BC36251331}.Release|Any CPU.ActiveCfg = Release|Any CPU
{73FE0BC7-3E61-4093-864D-77BC36251331}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1FF6D9EE-60E6-40F2-94B4-AC0FD9D92EA3}
EndGlobalSection
EndGlobal
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Find_Bookmark_Owner_TextBody_or_Header_Footer</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Syncfusion.DocIO.DLS;

namespace Find_Bookmark_Owner_TextBody_or_Header_Footer
{
class Program
{
public static void Main(string[] args)
{
// Load the existing Word document
WordDocument document = new WordDocument(Path.GetFullPath("Data/Template.docx"));
// Iterate through all bookmarks in the document.
foreach (Bookmark bookmark in document.Bookmarks)
{
// Get bookmark start from the current bookmark
BookmarkStart bkmkStart = bookmark.BookmarkStart;
if (bkmkStart != null)
{
// Get the paragraphs that contain the bookmark's start.
Entity ownerEntity = bkmkStart.OwnerParagraph;
// Traverse the owner hierarchy until reaching the section, stopping if a HeaderFooter is found
while (!(ownerEntity is WSection))
{
if (ownerEntity.EntityType == EntityType.HeaderFooter)
break;
ownerEntity = ownerEntity.Owner;
}
// Check if the bookmark is in the text body, header, or footer
string ownerLabel = (ownerEntity.EntityType == EntityType.Section)
? "TextBody"
: CheckHeaderFooterType(ownerEntity.Owner as WSection, ownerEntity as HeaderFooter);
// Print the bookmark name and its owner type
Console.WriteLine("Bookmark Name:" + bkmkStart.Name + "\n Bookmark Owner:" + ownerLabel);
}
}
Console.ReadLine();
}
/// <summary>
/// Returns a whether the provided HeaderFooter instance belongs to the header or footer of the given section.
/// </summary>
/// <param name="section">The section that contains the HeaderFooter</param>
/// <param name="headerFooter">The HeaderFooter instance to check.</param>
/// <returns>Returns "Header" if the instance is a header, "Footer" if it is a footer,
/// otherwise "Header and Footer".</returns>
private static string CheckHeaderFooterType(WSection section, HeaderFooter headerFooter)
{
string type = "Header and Footer";
// Check if the given HeaderFooter instance is one of the section's header references.
if (section.HeadersFooters.OddHeader == headerFooter
|| section.HeadersFooters.FirstPageHeader == headerFooter || section.HeadersFooters.EvenHeader == headerFooter)
{
type = "Header";
}
// Otherwise, check if it is one of the section's footer references.
else if (section.HeadersFooters.OddFooter == headerFooter || section.HeadersFooters.EvenFooter == headerFooter
|| section.HeadersFooters.FirstPageFooter == headerFooter)
{
type = "Footer";
}
return type;
}
}
}
Loading