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?
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’?
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);
}
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.