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

Free Support Forum - aspose.com

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));
i++;
for (Bookmark bookmark : interimdoc.getRange().getBookmarks()) {
if (bookmark.getName().startsWith(“Image_”)) {
bookmark.setText(str);
}
}

Regards.,
S S Vadivel

@Vadivel_S_S,

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);
builder.startBookmark("Bookmark");

//move the cursor to the end of second paragraph
builder.moveToParagraph(1, -1);
builder.endBookmark("Bookmark");
doc.save(MyDir + "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.moveTo(table.getFirstRow().getFirstCell().getFirstChild());
builder.startBookmark("Bookmark");
System.out.println(table.getLastRow().getLastCell().getLastChild());
builder.moveTo(table.getLastRow().getLastCell().getLastChild());
builder.endBookmark("Bookmark");
doc.save(MyDir + "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:
para.zip (1.5 KB)

Already I get solution for shapes:Solution.zip (1.1 KB)

Regards.,
S S Vadivel

@Vadivel_S_S,

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 : input.zip (333.1 KB)
Expected Output : Expected_Output.zip (10.2 KB)

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

Thanks&Regards.,
S S Vadivel

@Vadivel_S_S,

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.
output.zip (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>");
            run.getFont().setColor(Color.RED);
            p.getRuns().add(run);
            p.appendChild(new BookmarkEnd(doc, "MyBookmark"));
        }
    }
}

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

Hi tahir.,

Thanks your quick response. Its working fine.

Thanks & Regards.,
S S Vadivel