diff --git a/Bookmarks/Find-Bookmark-Owner-TextBody-or-Header-Footer/.NET/Find-Bookmark-Owner-TextBody-or-Header-Footer.sln b/Bookmarks/Find-Bookmark-Owner-TextBody-or-Header-Footer/.NET/Find-Bookmark-Owner-TextBody-or-Header-Footer.sln new file mode 100644 index 00000000..2ac7e6b7 --- /dev/null +++ b/Bookmarks/Find-Bookmark-Owner-TextBody-or-Header-Footer/.NET/Find-Bookmark-Owner-TextBody-or-Header-Footer.sln @@ -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 diff --git a/Bookmarks/Find-Bookmark-Owner-TextBody-or-Header-Footer/.NET/Find-Bookmark-Owner-TextBody-or-Header-Footer/Data/Template.docx b/Bookmarks/Find-Bookmark-Owner-TextBody-or-Header-Footer/.NET/Find-Bookmark-Owner-TextBody-or-Header-Footer/Data/Template.docx new file mode 100644 index 00000000..a82e1b95 Binary files /dev/null and b/Bookmarks/Find-Bookmark-Owner-TextBody-or-Header-Footer/.NET/Find-Bookmark-Owner-TextBody-or-Header-Footer/Data/Template.docx differ diff --git a/Bookmarks/Find-Bookmark-Owner-TextBody-or-Header-Footer/.NET/Find-Bookmark-Owner-TextBody-or-Header-Footer/Find-Bookmark-Owner-TextBody-or-Header-Footer.csproj b/Bookmarks/Find-Bookmark-Owner-TextBody-or-Header-Footer/.NET/Find-Bookmark-Owner-TextBody-or-Header-Footer/Find-Bookmark-Owner-TextBody-or-Header-Footer.csproj new file mode 100644 index 00000000..4e37f18e --- /dev/null +++ b/Bookmarks/Find-Bookmark-Owner-TextBody-or-Header-Footer/.NET/Find-Bookmark-Owner-TextBody-or-Header-Footer/Find-Bookmark-Owner-TextBody-or-Header-Footer.csproj @@ -0,0 +1,21 @@ + + + + Exe + net8.0 + Find_Bookmark_Owner_TextBody_or_Header_Footer + enable + enable + + + + + + + + + Always + + + + diff --git a/Bookmarks/Find-Bookmark-Owner-TextBody-or-Header-Footer/.NET/Find-Bookmark-Owner-TextBody-or-Header-Footer/Program.cs b/Bookmarks/Find-Bookmark-Owner-TextBody-or-Header-Footer/.NET/Find-Bookmark-Owner-TextBody-or-Header-Footer/Program.cs new file mode 100644 index 00000000..04ce6bb5 --- /dev/null +++ b/Bookmarks/Find-Bookmark-Owner-TextBody-or-Header-Footer/.NET/Find-Bookmark-Owner-TextBody-or-Header-Footer/Program.cs @@ -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(); + } + /// + /// Returns a whether the provided HeaderFooter instance belongs to the header or footer of the given section. + /// + /// The section that contains the HeaderFooter + /// The HeaderFooter instance to check. + /// Returns "Header" if the instance is a header, "Footer" if it is a footer, + /// otherwise "Header and Footer". + 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; + } + } +}