|
| 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 | + Entity ownerEntity = bkmkStart.OwnerParagraph; |
| 20 | + // Traverse the owner hierarchy until reaching the section, stopping if a HeaderFooter is found |
| 21 | + while (!(ownerEntity is WSection)) |
| 22 | + { |
| 23 | + if (ownerEntity.EntityType == EntityType.HeaderFooter) |
| 24 | + break; |
| 25 | + ownerEntity = ownerEntity.Owner; |
| 26 | + } |
| 27 | + // Check if the bookmark is in the text body, header, or footer |
| 28 | + string ownerLabel = (ownerEntity.EntityType == EntityType.Section) |
| 29 | + ? "TextBody" |
| 30 | + : CheckHeaderFooterType(ownerEntity.Owner as WSection, ownerEntity as HeaderFooter); |
| 31 | + // Print the bookmark name and its owner type |
| 32 | + Console.WriteLine("Bookmark Name:" + bkmkStart.Name + "\n Bookmark Owner:" + ownerLabel); |
| 33 | + } |
| 34 | + } |
| 35 | + Console.ReadLine(); |
| 36 | + } |
| 37 | + /// <summary> |
| 38 | + /// Returns a whether the provided HeaderFooter instance belongs to the header or footer of the given section. |
| 39 | + /// </summary> |
| 40 | + /// <param name="section">The section that contains the HeaderFooter</param> |
| 41 | + /// <param name="headerFooter">The HeaderFooter instance to check.</param> |
| 42 | + /// <returns>Returns "Header" if the instance is a header, "Footer" if it is a footer, |
| 43 | + /// otherwise "Header and Footer".</returns> |
| 44 | + private static string CheckHeaderFooterType(WSection section, HeaderFooter headerFooter) |
| 45 | + { |
| 46 | + string type = "Header and Footer"; |
| 47 | + // Check if the given HeaderFooter instance is one of the section's header references. |
| 48 | + if (section.HeadersFooters.OddHeader == headerFooter |
| 49 | + || section.HeadersFooters.FirstPageHeader == headerFooter || section.HeadersFooters.EvenHeader == headerFooter) |
| 50 | + { |
| 51 | + type = "Header"; |
| 52 | + } |
| 53 | + // Otherwise, check if it is one of the section's footer references. |
| 54 | + else if (section.HeadersFooters.OddFooter == headerFooter || section.HeadersFooters.EvenFooter == headerFooter |
| 55 | + || section.HeadersFooters.FirstPageFooter == headerFooter) |
| 56 | + { |
| 57 | + type = "Footer"; |
| 58 | + } |
| 59 | + return type; |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments