How to apply bookmark to Paragraph and table section

Dear team,

How to apply bookmark to Paragraph and tables section using aspose.words java.

I already applied book mark to Shape object. At the same how to apply bookmark to paragraph and tables. Here I have attached sample code for bookmark into Shape object.

String str=“Styles”;
shape.getParentParagraph().insertBefore(new BookmarkStart(interimdoc1, “Image_” + i), shape);
para.appendChild(new BookmarkEnd(interimdoc1, “Image_” + i));
for (Bookmark bookmark : interimdoc.getRange().getBookmarks()) {
if (bookmark.getName().startsWith(“Image_”)) {

S S Vadivel


Thanks for your inquiry. In your case, we suggest you please move the cursor to the desired location e.g. paragraph or first cell of table and insert the bookmark. You may use CompositeNode.InsertBefore and CompositeNode.InsertAfter methods to insert the BookmarkStart and BookmarkEnd node to paragraph and table node.

Following code example shows how to bookmark the second paragraph of document.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
//move the cursor to the start of second paragraph
builder.moveToParagraph(1, 0);

//move the cursor to the end of second paragraph
builder.moveToParagraph(1, -1);
builder.endBookmark("Bookmark"); + "output.doc");

Following code example shows how to bookmark the table.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
builder.endBookmark("Bookmark"); + "output.docx");

Hi tahir.,

Thanks for your responses.
I have attached my code for your reference, Please give me solution for this. Once I removed images from document, after removed imaging place we add some name. like Num_Figure_1.
I find solution for shapes object.But not get solution for paragraph and tables.

Plz give solution for this code: (1.5 KB)

Already I get solution for (1.1 KB)

S S Vadivel


Thanks for your inquiry. Please ZIP and attach your input and expected output documents here for our reference. We will then provide you more information on this along with code.

Hi tahir.,

Thanks for your response.
Input : (333.1 KB)
Expected Output : (10.2 KB)

I have attached code for paragraph and table section please give the solution for this code.
Code : (1.5 KB)

S S Vadivel


Thanks for sharing the documents. Unfortunately, your query is not clear enough. As per your first post, you want to bookmark the paragraphs and tables. In your expected output document, there is no bookmark except “OLE_LINK16” that exists in input document as well.

As per my understanding, you want to extract the Shape and Table nodes from the input document and replace them with some text e.g. “<<Fig>Num_Fig_1</Fig>”. If this is the case, you can achieve this using following code example. This code example replaces Shape node with text. Please do the same for Table node. We have attached the output document with this post for your kind reference. Hope this helps you. (9.0 KB)

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

for (Paragraph paragraph : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)) {
    if (paragraph.toString(SaveFormat.TEXT).trim().startsWith("Fig")) {
        Node previousPara = paragraph.getPreviousSibling();
        while (previousPara != null && previousPara.getNodeType() == NodeType.PARAGRAPH
                && previousPara.toString(SaveFormat.TEXT).trim().length() == 0
                && ((Paragraph) previousPara).getChildNodes(NodeType.SHAPE, true).getCount() > 0) {

            ((Paragraph) previousPara).getChildNodes(NodeType.SHAPE, true).clear();

            Paragraph p = ((Paragraph) previousPara);
            p.getChildNodes(NodeType.SHAPE, true).clear();
            p.appendChild(new BookmarkStart(doc, "MyBookmark"));
            Run run = new Run(doc, "<Fig>Num_Fig_1</Fig>");
            p.appendChild(new BookmarkEnd(doc, "MyBookmark"));
} + "output.docx");

Hi tahir.,

Thanks your quick response. Its working fine.

Thanks & Regards.,
S S Vadivel