How to bookmark a cell in a table in java?

image.png (2.0 KB)
if I’ve a table in a word document say

1st row -> abc | 123
--------------
2nd row -> def | 456

How can we bookmark “456” using aspose java API ?

@vgm,

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.

You need to insert BookmarkStart node before and BookmarkEnd node after the desired content. Following code example shows how to bookmark the first paragraph of second cell in first row of table. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");

Table table = doc.getFirstSection().getBody().getTables().get(0);
Paragraph p = table.getFirstRow().getCells().get(1).getFirstParagraph();
             
p.appendChild(new BookmarkStart(doc, "bookmark"));
p.appendChild(new BookmarkEnd(doc, "bookmark"));
              
doc.save(MyDir + "18.6.docx");

Thanks Tahir for your reply. When I do goto “bookmark” in the word document, it just places the cursor in that cell. How can we add bookmark in such a way that the entire content of the cell gets selected when we attempt goto-> Bookmark -> bookmark_name?

@vgm,

Thanks for your inquiry. We suggest you please read about Aspose.Words document object model from following link.
Aspose.Words Document Object Model

Aspose.Words for Java is just a class library and with it you can programmatically generate, modify, convert, render and print documents without utilizing Microsoft Word®. So, it does not offer any UI for performing document processing tasks.

Following code example shows how to bookmark the content of table’s cell.

Document doc = new Document(MyDir + "in.docx");

Table table = doc.getFirstSection().getBody().getTables().get(0);
Cell cell = table.getFirstRow().getCells().get(1);

cell.getFirstParagraph().insertBefore(new BookmarkStart(doc, "bookmark"), cell.getFirstParagraph().getFirstChild());
cell.getLastParagraph().insertAfter(new BookmarkEnd(doc, "bookmark"), cell.getLastParagraph().getLastChild());

doc.save(MyDir + "18.6.docx");