From 13eb25b96435c8c62e7ddfde709d420f53ad2803 Mon Sep 17 00:00:00 2001 From: KB Bot Date: Tue, 16 Jul 2024 13:17:43 +0000 Subject: [PATCH 1/3] Added new kb article radwordsprocessing-find-table-by-bookmark --- ...dwordsprocessing-find-table-by-bookmark.md | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 knowledge-base/radwordsprocessing-find-table-by-bookmark.md diff --git a/knowledge-base/radwordsprocessing-find-table-by-bookmark.md b/knowledge-base/radwordsprocessing-find-table-by-bookmark.md new file mode 100644 index 00000000..8c066d01 --- /dev/null +++ b/knowledge-base/radwordsprocessing-find-table-by-bookmark.md @@ -0,0 +1,103 @@ +--- +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 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. + +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 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. + +1. **Load the document and identify the bookmark:** + +```csharp +RadFlowDocument document; +DocxFormatProvider docxFormatProvider = new DocxFormatProvider(); + +using (Stream input = File.OpenRead("input.docx")) +{ + document = docxFormatProvider.Import(input); +} + +Bookmark bookmark = document.EnumerateChildrenOfType().Select(b => b.Bookmark).ToList().First(bm => bm.Name == "BookmarkName"); +``` + +2. **Define methods to get the innermost and outermost tables containing the bookmark:** + +- **GetInnermostTableContainingBookmark:** + +```csharp +private static Table GetInnermostTableContainingBookmark(Bookmark bookmark) +{ + TableCell tableCell = bookmark.BookmarkRangeStart.Paragraph.BlockContainer as TableCell; + + if (tableCell != null) + { + return tableCell.Table; + } + + return null; +} +``` + +- **GetOutermostTableContainingBookmark:** + +```csharp +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; +} +``` + +3. **Retrieve the innermost and outermost tables containing the bookmark (as needed):** + +```csharp +Table innermostTable = GetInnermostTableContainingBookmark(bookmark); +Table outermostTable = GetOutermostTableContainingBookmark(bookmark); +``` + +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) From ad841b1f6ceae944fb0db952ad69b604792214ee Mon Sep 17 00:00:00 2001 From: "PROGRESS\\ykaraman" Date: Wed, 17 Jul 2024 13:02:02 +0300 Subject: [PATCH 2/3] referenced KB in other articles --- knowledge-base/radwordsprocessing-find-table-by-bookmark.md | 1 + libraries/radwordsprocessing/model/bookmark.md | 1 + libraries/radwordsprocessing/model/table.md | 1 + 3 files changed, 3 insertions(+) diff --git a/knowledge-base/radwordsprocessing-find-table-by-bookmark.md b/knowledge-base/radwordsprocessing-find-table-by-bookmark.md index 8c066d01..446c5308 100644 --- a/knowledge-base/radwordsprocessing-find-table-by-bookmark.md +++ b/knowledge-base/radwordsprocessing-find-table-by-bookmark.md @@ -101,3 +101,4 @@ If the bookmark is in a single table, both methods will yield the same result. T ## See Also - [RadWordsProcessing Documentation](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/overview) +- [Bookmark]({%slug radwordsprocessing-model-bookmark%}) diff --git a/libraries/radwordsprocessing/model/bookmark.md b/libraries/radwordsprocessing/model/bookmark.md index 0284a42b..c241ee56 100644 --- a/libraries/radwordsprocessing/model/bookmark.md +++ b/libraries/radwordsprocessing/model/bookmark.md @@ -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%}) diff --git a/libraries/radwordsprocessing/model/table.md b/libraries/radwordsprocessing/model/table.md index b7366d01..e5346141 100644 --- a/libraries/radwordsprocessing/model/table.md +++ b/libraries/radwordsprocessing/model/table.md @@ -178,3 +178,4 @@ __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%}) From 5d3cb0602c68b7f27664c11aa0238917871c3f91 Mon Sep 17 00:00:00 2001 From: Yoan Karamanov Date: Mon, 12 Aug 2024 12:54:31 +0300 Subject: [PATCH 3/3] Resolve comments --- ...dwordsprocessing-find-table-by-bookmark.md | 28 ++++++------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/knowledge-base/radwordsprocessing-find-table-by-bookmark.md b/knowledge-base/radwordsprocessing-find-table-by-bookmark.md index 446c5308..cc8a977c 100644 --- a/knowledge-base/radwordsprocessing-find-table-by-bookmark.md +++ b/knowledge-base/radwordsprocessing-find-table-by-bookmark.md @@ -4,7 +4,7 @@ description: This article demonstrates how to identify and retrieve the table th 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 +tags: radwordsprocessing, document, processing, bookmarks, table, nested, tables res_type: kb ticketid: 1657970 --- @@ -17,7 +17,7 @@ ticketid: 1657970 ## Description -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. +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? @@ -26,9 +26,13 @@ This KB article also answers the following questions: ## Solution -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. +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; @@ -40,13 +44,10 @@ using (Stream input = File.OpenRead("input.docx")) } Bookmark bookmark = document.EnumerateChildrenOfType().Select(b => b.Bookmark).ToList().First(bm => bm.Name == "BookmarkName"); -``` -2. **Define methods to get the innermost and outermost tables containing the bookmark:** - -- **GetInnermostTableContainingBookmark:** +Table innermostTable = GetInnermostTableContainingBookmark(bookmark); +Table outermostTable = GetOutermostTableContainingBookmark(bookmark); -```csharp private static Table GetInnermostTableContainingBookmark(Bookmark bookmark) { TableCell tableCell = bookmark.BookmarkRangeStart.Paragraph.BlockContainer as TableCell; @@ -58,11 +59,7 @@ private static Table GetInnermostTableContainingBookmark(Bookmark bookmark) return null; } -``` - -- **GetOutermostTableContainingBookmark:** -```csharp private static Table GetOutermostTableContainingBookmark(Bookmark bookmark) { TableCell tableCell = bookmark.BookmarkRangeStart.Paragraph.BlockContainer as TableCell; @@ -89,13 +86,6 @@ private static Table GetTableContainingAnotherTable(Table table) } ``` -3. **Retrieve the innermost and outermost tables containing the bookmark (as needed):** - -```csharp -Table innermostTable = GetInnermostTableContainingBookmark(bookmark); -Table outermostTable = GetOutermostTableContainingBookmark(bookmark); -``` - 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