|
| 1 | +--- |
| 2 | +title: Finding a Table Containing a Specific Bookmark in Word Documents |
| 3 | +description: This article demonstrates how to identify and retrieve the table that contains a specific bookmark within a document using WordsProcessing. |
| 4 | +type: how-to |
| 5 | +page_title: How to Retrieve a Table by Bookmark in RadWordsProcessing |
| 6 | +slug: radwordsprocessing-find-table-by-bookmark |
| 7 | +tags: radwordsprocessing, document, processing, bookmarks, table, nested, tables |
| 8 | +res_type: kb |
| 9 | +ticketid: 1657970 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 2024.2.426| RadWordsProcessing |[Yoan Karamanov](https://www.telerik.com/blogs/author/yoan-karamanov)| |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +When working with documents, it's a common requirement to find a table that contains a specific bookmark. This can become complex when dealing with nested tables, as a bookmark could be situated within multiple layers of tables. This KB article outlines methods to find either the innermost or outermost table containing a given bookmark, catering to scenarios involving nested tables. |
| 21 | + |
| 22 | +This KB article also answers the following questions: |
| 23 | +- How can I find a table containing a specific bookmark in a document? |
| 24 | +- What method can I use to retrieve the innermost table with a bookmark in RadWordsProcessing? |
| 25 | +- How do I determine the outermost table that includes a specific bookmark in nested table scenarios? |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +To find a table containing a specific bookmark, especially in documents with nested tables, you can use the following custom methods: `GetInnermostTableContainingBookmark` and `GetOutermostTableContainingBookmark`. These methods help in identifying either the innermost or outermost table that contains the bookmark, depending on the nesting level of tables in the document. |
| 30 | + |
| 31 | +1. **Load the document and identify the bookmark:** |
| 32 | +2. **Define methods to get the innermost and outermost tables containing the bookmark:** |
| 33 | +- **GetInnermostTableContainingBookmark:** |
| 34 | +- **GetOutermostTableContainingBookmark:** |
| 35 | +3. **Retrieve the innermost and outermost tables containing the bookmark (as needed):** |
| 36 | + |
| 37 | +```csharp |
| 38 | +RadFlowDocument document; |
| 39 | +DocxFormatProvider docxFormatProvider = new DocxFormatProvider(); |
| 40 | + |
| 41 | +using (Stream input = File.OpenRead("input.docx")) |
| 42 | +{ |
| 43 | + document = docxFormatProvider.Import(input); |
| 44 | +} |
| 45 | + |
| 46 | +Bookmark bookmark = document.EnumerateChildrenOfType<BookmarkRangeStart>().Select(b => b.Bookmark).ToList().First(bm => bm.Name == "BookmarkName"); |
| 47 | + |
| 48 | +Table innermostTable = GetInnermostTableContainingBookmark(bookmark); |
| 49 | +Table outermostTable = GetOutermostTableContainingBookmark(bookmark); |
| 50 | + |
| 51 | +private static Table GetInnermostTableContainingBookmark(Bookmark bookmark) |
| 52 | +{ |
| 53 | + TableCell tableCell = bookmark.BookmarkRangeStart.Paragraph.BlockContainer as TableCell; |
| 54 | + |
| 55 | + if (tableCell != null) |
| 56 | + { |
| 57 | + return tableCell.Table; |
| 58 | + } |
| 59 | + |
| 60 | + return null; |
| 61 | +} |
| 62 | + |
| 63 | +private static Table GetOutermostTableContainingBookmark(Bookmark bookmark) |
| 64 | +{ |
| 65 | + TableCell tableCell = bookmark.BookmarkRangeStart.Paragraph.BlockContainer as TableCell; |
| 66 | + |
| 67 | + if (tableCell != null) |
| 68 | + { |
| 69 | + Table table = tableCell.Table; |
| 70 | + return GetTableContainingAnotherTable(table); |
| 71 | + } |
| 72 | + |
| 73 | + return null; |
| 74 | +} |
| 75 | + |
| 76 | +private static Table GetTableContainingAnotherTable(Table table) |
| 77 | +{ |
| 78 | + TableCell cell = table.BlockContainer as TableCell; |
| 79 | + |
| 80 | + if (cell != null) |
| 81 | + { |
| 82 | + return GetTableContainingAnotherTable(cell.Table); |
| 83 | + } |
| 84 | + |
| 85 | + return table; |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +If the bookmark is in a single table, both methods will yield the same result. These methods ensure you can accurately find the table containing a specific bookmark, regardless of the complexity of the document's table structure. |
| 90 | + |
| 91 | +## See Also |
| 92 | + |
| 93 | +- [RadWordsProcessing Documentation](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/overview) |
| 94 | +- [Bookmark]({%slug radwordsprocessing-model-bookmark%}) |
0 commit comments