Skip to content

Commit 9cc5035

Browse files
authored
Merge pull request #445 from telerik/new-kb-radwordsprocessing-find-table-by-bookmark-faa605e770b34efbb917964e9b91d58b
Added new kb article radwordsprocessing-find-table-by-bookmark
2 parents f09d4ef + 5d3cb06 commit 9cc5035

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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%})

libraries/radwordsprocessing/model/bookmark.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,4 @@ __Example 3__ demonstrates how you can delete the bookmark created in __Example
7272

7373
* [Paragraph]({%slug radwordsprocessing-model-paragraph%})
7474
* [RadFlowDocumentEditor]({%slug radwordsprocessing-editing-radflowdocumenteditor%})
75+
* [Finding a Table Containing a Specific Bookmark in Word Documents]({%slug radwordsprocessing-find-table-by-bookmark%})

libraries/radwordsprocessing/model/table.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,6 @@ __Example 5__ demonstrates how to add a __Table__ with 5 rows and 10 columns to
178178
* [TableRow]({%slug radwordsprocessing-model-tablerow%})
179179
* [TableCell]({%slug radwordsprocessing-model-tablecell%})
180180
* [Style Properties]({%slug radwordsprocessing-concepts-style-properties%})
181+
* [Finding a Table Containing a Specific Bookmark in Word Documents]({%slug radwordsprocessing-find-table-by-bookmark%})
181182
* [Generating Dynamic DOCX Documents with Tables and CheckBoxes using RadWordsProcessing]({%slug dynamic-docx-document-generation-radwordsprocessing%})
183+

0 commit comments

Comments
 (0)