We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to bookmark part of a cell in a word document table using java?

If a cell in a table has value " abcd ", is it possible to bookmark just the non-space part of the value like “abcd” or “bc”? can you please provide sample code?

Thanks.

@vgm,

Thanks for your inquiry. Yes, this is possible using Aspose.Words. You need to insert the BookmarkStart and BookmarkEnd nodes before and after the desired text respectively. Please note that all text of the document is stored in runs of text. The text “abcd” can be in one or multiple Run nodes. You need to split the text into Run nodes and insert the bookmark.

We suggest you please read the following article.
Aspose.Words Document Object Model

Thanks Tahir.

I went through the link provided. I tried retrieving the run nodes for a cell. It almost always is 1.
I can add bookmark to a part of cell text(which has one run node) in the word document in the UI, how can I achieve the same programmatically using aspose?

@vgm

Thanks for your inquiry. We suggest you please read the following article.
Find and Replace

Following code example shows how to find the text “bc” from a table’s cell and bookmark it. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
Cell cell = doc.getFirstSection().getBody().getTables().get(0).getFirstRow().getFirstCell();
FindReplaceOptions options = new FindReplaceOptions();
options.setReplacingCallback(new FindAndInsertBookmark());
cell.getRange().replace("bc", "", options);
doc.save(MyDir + "18.8.docx");

class FindAndInsertBookmark implements IReplacingCallback {

    public int replacing(ReplacingArgs e) throws Exception {
        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = e.getMatchNode();

        // The first (and may be the only) run can contain text before the match,
        // in this case it is necessary to split the run.
        if (e.getMatchOffset() > 0)
            currentNode = splitRun((Run) currentNode, e.getMatchOffset());

        ArrayList runs = new ArrayList();

        // Find all runs that contain parts of the match string.
        int remainingLength = e.getMatch().group().length();
        while ((remainingLength > 0) && (currentNode != null) && (currentNode.getText().length() <= remainingLength)) {
            runs.add(currentNode);
            remainingLength = remainingLength - currentNode.getText().length();

            // Select the next Run node.
            // Have to loop because there could be other nodes such as BookmarkStart etc.
            do {
                currentNode = currentNode.getNextSibling();
            } while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));
        }

        // Split the last run that contains the match if there is any text left.
        if ((currentNode != null) && (remainingLength > 0)) {
            splitRun((Run) currentNode, remainingLength);
            runs.add(currentNode);
        }

        Document document = (Document) e.getMatchNode().getDocument();
        DocumentBuilder builder = new DocumentBuilder(document);
        Run run = (Run)runs.get(0);

        run.getParentParagraph().insertBefore(new BookmarkStart(document, "bookmark"), run);
        run.getParentParagraph().insertAfter(new BookmarkEnd(document, "bookmark"), run);
        return ReplaceAction.SKIP;
    }

    private  Run splitRun(Run run, int position) throws Exception {
        Run afterRun = (Run) run.deepClone(true);
        afterRun.setText(run.getText().substring(position));
        run.setText(run.getText().substring((0), (0) + (position)));
        run.getParentNode().insertAfter(afterRun, run);
        return afterRun;
    }
}