Finding a Table Containing a Specific Bookmark in Word Documents
Environment
| Version | Product | Author |
|---|---|---|
| 2024.2.426 | RadWordsProcessing | Yoan Karamanov |
Description
When working with documents, finding a table that contains a specific bookmark is a common requirement. This task can become complex with nested tables, as a bookmark can reside within multiple layers of tables. This article outlines methods to find either the innermost or outermost table that contains a given bookmark.
This 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 in documents with nested tables, use the following custom methods: GetInnermostTableContainingBookmark and GetOutermostTableContainingBookmark. These methods identify either the innermost or outermost table that contains the bookmark, depending on the nesting level.
- Load the document and identify the bookmark:
- Define methods to get the innermost and outermost tables containing the bookmark:
- GetInnermostTableContainingBookmark:
- GetOutermostTableContainingBookmark:
- Retrieve the innermost and outermost tables containing the bookmark (as needed):
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 yield the same result. These methods ensure accurate table retrieval regardless of the document table structure complexity.