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
94 changes: 94 additions & 0 deletions knowledge-base/radwordsprocessing-find-table-by-bookmark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
title: Finding a Table Containing a Specific Bookmark in Word Documents
description: This article demonstrates how to identify and retrieve the table that contains a specific bookmark within a document using WordsProcessing.
type: how-to
page_title: How to Retrieve a Table by Bookmark in RadWordsProcessing
slug: radwordsprocessing-find-table-by-bookmark
tags: radwordsprocessing, document, processing, bookmarks, table, nested, tables
res_type: kb
ticketid: 1657970
---

## Environment

| Version | Product | Author |
| --- | --- | ---- |
| 2024.2.426| RadWordsProcessing |[Yoan Karamanov](https://www.telerik.com/blogs/author/yoan-karamanov)|

## Description

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.

This KB article also answers the following questions:
- How can I find a table containing a specific bookmark in a document?
- What method can I use to retrieve the innermost table with a bookmark in RadWordsProcessing?
- How do I determine the outermost table that includes a specific bookmark in nested table scenarios?

## Solution

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.

1. **Load the document and identify the bookmark:**
2. **Define methods to get the innermost and outermost tables containing the bookmark:**
- **GetInnermostTableContainingBookmark:**
- **GetOutermostTableContainingBookmark:**
3. **Retrieve the innermost and outermost tables containing the bookmark (as needed):**

```csharp
RadFlowDocument document;
DocxFormatProvider docxFormatProvider = new DocxFormatProvider();

using (Stream input = File.OpenRead("input.docx"))
{
document = docxFormatProvider.Import(input);
}

Bookmark bookmark = document.EnumerateChildrenOfType<BookmarkRangeStart>().Select(b => b.Bookmark).ToList().First(bm => bm.Name == "BookmarkName");

Table innermostTable = GetInnermostTableContainingBookmark(bookmark);
Table outermostTable = GetOutermostTableContainingBookmark(bookmark);

private static Table GetInnermostTableContainingBookmark(Bookmark bookmark)
{
TableCell tableCell = bookmark.BookmarkRangeStart.Paragraph.BlockContainer as TableCell;

if (tableCell != null)
{
return tableCell.Table;
}

return null;
}

private static Table GetOutermostTableContainingBookmark(Bookmark bookmark)
{
TableCell tableCell = bookmark.BookmarkRangeStart.Paragraph.BlockContainer as TableCell;

if (tableCell != null)
{
Table table = tableCell.Table;
return GetTableContainingAnotherTable(table);
}

return null;
}

private static Table GetTableContainingAnotherTable(Table table)
{
TableCell cell = table.BlockContainer as TableCell;

if (cell != null)
{
return GetTableContainingAnotherTable(cell.Table);
}

return table;
}
```

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.

## See Also

- [RadWordsProcessing Documentation](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/overview)
- [Bookmark]({%slug radwordsprocessing-model-bookmark%})
1 change: 1 addition & 0 deletions libraries/radwordsprocessing/model/bookmark.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,4 @@ __Example 3__ demonstrates how you can delete the bookmark created in __Example

* [Paragraph]({%slug radwordsprocessing-model-paragraph%})
* [RadFlowDocumentEditor]({%slug radwordsprocessing-editing-radflowdocumenteditor%})
* [Finding a Table Containing a Specific Bookmark in Word Documents]({%slug radwordsprocessing-find-table-by-bookmark%})
2 changes: 2 additions & 0 deletions libraries/radwordsprocessing/model/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,6 @@ __Example 5__ demonstrates how to add a __Table__ with 5 rows and 10 columns to
* [TableRow]({%slug radwordsprocessing-model-tablerow%})
* [TableCell]({%slug radwordsprocessing-model-tablecell%})
* [Style Properties]({%slug radwordsprocessing-concepts-style-properties%})
* [Finding a Table Containing a Specific Bookmark in Word Documents]({%slug radwordsprocessing-find-table-by-bookmark%})
* [Generating Dynamic DOCX Documents with Tables and CheckBoxes using RadWordsProcessing]({%slug dynamic-docx-document-generation-radwordsprocessing%})