Skip to content

Commit 13eb25b

Browse files
author
KB Bot
committed
Added new kb article radwordsprocessing-find-table-by-bookmark
1 parent 0d0baa3 commit 13eb25b

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 common to need 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 two 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+
33+
```csharp
34+
RadFlowDocument document;
35+
DocxFormatProvider docxFormatProvider = new DocxFormatProvider();
36+
37+
using (Stream input = File.OpenRead("input.docx"))
38+
{
39+
document = docxFormatProvider.Import(input);
40+
}
41+
42+
Bookmark bookmark = document.EnumerateChildrenOfType<BookmarkRangeStart>().Select(b => b.Bookmark).ToList().First(bm => bm.Name == "BookmarkName");
43+
```
44+
45+
2. **Define methods to get the innermost and outermost tables containing the bookmark:**
46+
47+
- **GetInnermostTableContainingBookmark:**
48+
49+
```csharp
50+
private static Table GetInnermostTableContainingBookmark(Bookmark bookmark)
51+
{
52+
TableCell tableCell = bookmark.BookmarkRangeStart.Paragraph.BlockContainer as TableCell;
53+
54+
if (tableCell != null)
55+
{
56+
return tableCell.Table;
57+
}
58+
59+
return null;
60+
}
61+
```
62+
63+
- **GetOutermostTableContainingBookmark:**
64+
65+
```csharp
66+
private static Table GetOutermostTableContainingBookmark(Bookmark bookmark)
67+
{
68+
TableCell tableCell = bookmark.BookmarkRangeStart.Paragraph.BlockContainer as TableCell;
69+
70+
if (tableCell != null)
71+
{
72+
Table table = tableCell.Table;
73+
return GetTableContainingAnotherTable(table);
74+
}
75+
76+
return null;
77+
}
78+
79+
private static Table GetTableContainingAnotherTable(Table table)
80+
{
81+
TableCell cell = table.BlockContainer as TableCell;
82+
83+
if (cell != null)
84+
{
85+
return GetTableContainingAnotherTable(cell.Table);
86+
}
87+
88+
return table;
89+
}
90+
```
91+
92+
3. **Retrieve the innermost and outermost tables containing the bookmark (as needed):**
93+
94+
```csharp
95+
Table innermostTable = GetInnermostTableContainingBookmark(bookmark);
96+
Table outermostTable = GetOutermostTableContainingBookmark(bookmark);
97+
```
98+
99+
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.
100+
101+
## See Also
102+
103+
- [RadWordsProcessing Documentation](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/overview)

0 commit comments

Comments
 (0)