Table inside bookmark

Hi,

I have seen posts and solutions similar to this but not exactly what I need. How can I find a table inside a bookmark?

I have seen this post: https://forum.aspose.com/t/60048 which finds a table with a bookmark inside, but I need the reverse.

I cannot find a child of a bookmark and the NextSibling only shows a Run within it (not the table).

I have seen a way to extract data within a bookmark https://docs.aspose.com/words/net/how-to-extract-selected-content-between-nodes-in-a-document/ however I want to work within the table rather than make a clone of it elsewhere.

My design uses a bookmark to contain text and tables relevant to a specific condition. Currently I have logic that removes the bookmark text (including tables) conditionally. I would prefer not to have a second bookmark inside the table in order to identify it.

Any ideas? Can I see a table inside a bookmark in any way?

Cheers,
Brett

Hi Brett,

Thanks for your inquiry. Bookmark is a “facade” object that encapsulates two nodes BookmarkStart and BookmarkEnd in a document tree and allows to work with a bookmark as a single object.

Please use the following code example to get the table nodes inside a bookmark. Hope this helps you. Please let us know if you have any more queries.

var doc = new Document(MyDir + "Test01.docx");
Bookmark bm = doc.Range.Bookmarks["bm"];
Node currentNode = bm.BookmarkStart;
ArrayList tables = new ArrayList();
while (currentNode != bm.BookmarkEnd)
{
    if (currentNode.NodeType == NodeType.Table)
    {
        tables.Add(currentNode);
        currentNode = currentNode.NextSibling;
        continue;
    }
    currentNode = currentNode.NextPreOrder(doc);
}

Thank you! That seemed to work nicely.
Now how about getting a list of all ‘rows’ of a table within a bookmark? I tried to change your code to look for currentNode.NodeType == NodeType.Row but it doesn’t seem to find any rows. Do I need to be in a Table construct in order to see a ‘row’?

Hi Brett,

Thanks for your inquiry.

Please use the following code example to get
the Row nodes inside a bookmark. Hope this helps you. If you face any issue, please share your input document here for testing. I will investigate the issue and provide you more information.

var doc = new Document(MyDir + "Test01.docx");
Bookmark bm = doc.Range.Bookmarks["bm"];
Node currentNode = bm.BookmarkStart;
ArrayList rows = new ArrayList();
while (currentNode != bm.BookmarkEnd)
{
    if (currentNode.NodeType == NodeType.Row)
    {
        rows.Add(currentNode);
    }
    currentNode = currentNode.NextPreOrder(doc);
}

Hi,

I have attached my sample as it didnt’ work. The bookmark ContactRow is the one I am seeking to find a row in.

Thanks!
Brett

Hi Brett,

Thanks for sharing the document. In your case, the BookmarkStart node is inside table’s cell. Please check the attached DOM image for detail. Please use the following code snippet to achieve your requirements. Hope this helps you.

var doc = new Document(MyDir + "Bookmark+test+source.docx");
Bookmark bm = doc.Range.Bookmarks["ContactRow"];
Node currentNode = bm.BookmarkStart;
ArrayList rows = new ArrayList();
// get first row if BookmarkStart is inside tabe's cell
Node firstrow = currentNode;
if (currentNode.GetAncestor(NodeType.Cell) != null)
{
    firstrow = ((Cell)currentNode.GetAncestor(NodeType.Cell)).ParentRow;
    rows.Add(firstrow);
    currentNode = currentNode.NextPreOrder(doc);
}
while (currentNode != bm.BookmarkEnd)
{
    if (currentNode.NodeType == NodeType.Row)
    {
        if (!firstrow.Equals((Row)currentNode))
            rows.Add(currentNode);
    }
    currentNode = currentNode.NextPreOrder(doc);
}

Thank you that explains why I was having difficulty. And thank you for the solution and also reminding me what a useful tool the DocumentExplorer is. I found it via this post https://forum.aspose.com/t/59470 to download directly.

Good work Aspose!
Cheers,
Brett

Hi Brett,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.