How to insert bookmark in each paragraph?

Dear Team,

How to insert bookmark into each paragraph in word document using aspose.words in java.

I have attached expected output. Please find.

Expected output : exp.zip (9.7 KB)

@priyanga,

Please try using the following code:

Document doc = new Document("D:\\temp\\exp.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

int i = 0;
for(Paragraph para : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)){
    builder.moveTo(para);

    BookmarkStart start = builder.startBookmark("bm_" + i);
    BookmarkEnd end = builder.endBookmark("bm_" + i);

    if (para.getChildNodes().getCount() > 2) {
        para.insertBefore(start, para.getFirstChild());
    }

    i++;
}

doc.save("D:\\Temp\\awjava-18.2.docx");

Thanks for your quick reply. Its working fine. I have one more clarification. While bookmarking skip the tables, images,and page numbers.

And also skip empty bookmark.

Please give solution for this.

@priyanga,

Thanks for your inquiry. Please ZIP and attach the following resources here for testing:

  • Your input Word document.
  • Your expected Word document which shows the desired bookmarks. You can create this document by using Microsoft Word.

As soon as you get these pieces of information ready, we’ll start further investigation into your issue and provide you code to achieve the same expected output by using Aspose.Words for Java.

Thank you. I have attached documents. Please find.

Input : Input.zip (519.3 KB)
My Current Output : MyResult.zip (503.5 KB)
Expected Output : Exp_Output.zip (504.7 KB)

@priyanga,

Please try using the following code:

Document doc = new Document("D:\\Temp\\input\\input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

int i = 0;
for(Paragraph para : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)) {
    // Skip Paragraph if it is inside Table
    if (para.getAncestor(NodeType.TABLE) != null)
        continue;
    // Skip Paragraph if it contains a Shape
    if (para.getChildNodes(NodeType.SHAPE, true).getCount() > 0)
        continue;

    builder.moveTo(para);

    BookmarkStart start = builder.startBookmark("bm_" + i);
    BookmarkEnd end = builder.endBookmark("bm_" + i);

    if (para.getChildNodes().getCount() > 2) {
        para.insertBefore(start, para.getFirstChild());
    }

    i++;
}

doc.save("D:\\Temp\\input\\awjava-18.2.docx");

Thank you. Its working fine.

Dear awais,

I need to skip the bookmark for equations and author references in document file. please give solution for this.

@priyanga,

Thanks for your inquiry. Please ZIP and attach the following resources here for testing:

  • Your input Word document.
  • Your expected Word document which shows the desired bookmarks. You can create this document by using Microsoft Word.

As soon as you get these pieces of information ready, we’ll start further investigation into your issue and provide you code to achieve the same expected output by using Aspose.Words for Java.

Dear Awais,

I have attached sample current output and expected output for your reference.

Output : CurrentOutput.zip (12.2 KB)

Expected Output : OutDocument.zip (9.9 KB)

I need ignore the Equation types and document references.

@priyanga,

You can add one more check to skip Paragraphs if they contain Math Equation(s):

// Skip Paragraph if it contains a Math Equation
if (para.getChildNodes(NodeType.OFFICE_MATH, true).getCount() > 0)
    continue;

Dear Awais,

thanks for your quick replying. Its working. but i need to ignore only equation. But here full paragraph was ignored.

Sample : [ content " Equation " content ]

Expected : [ content content ]

@priyanga,

If Paragraph has one equation, do you want multiple bookmarks in a Paragraph i.e. bookmark text before equation and bookmark for text after equation? Please ZIP and attach your expected Word document which shows the desired bookmarks. You can create this document by using Microsoft Word.

Dear awais,

Request for how to start bookmark the content from Abstract or Introduction part.

Input : input.zip (26.2 KB)

Current OP : Current_Output.zip (26.4 KB)

Expected OP : Expected_Output.zip (26.1 KB)

@priyanga,

We are checking this scenario and will get back to you soon.

@priyanga,

Please try using the following code and see this output (awjava-18.3.zip (24.3 KB))

Document doc = new Document("D:\\Temp\\input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

boolean flag = false;
int i = 0;
for(Paragraph para : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)) {
    // Skip Paragraphs until 'Summary' is reached
    if (!flag) {
        if (!para.toString(SaveFormat.TEXT).trim().equals("Summary"))
            continue;
        else
            flag = true;
    }

    // Skip empty Paragraphs
    if (para.toString(SaveFormat.TEXT).trim().equals(""))
        continue;

    builder.moveTo(para);

    BookmarkStart start = builder.startBookmark("bm_" + i);
    BookmarkEnd end = builder.endBookmark("bm_" + i);

    if (para.getChildNodes().getCount() > 2) {
        para.insertBefore(start, para.getFirstChild());
    }

    i++;
}

doc.save("D:\\Temp\\awjava-18.3.docx");

Hi Awais,

Thanks lot. Its very useful for us.

Hi Awais,

I have one more skip of content in document. i need to skip para of Keywords section in document.

Please advice and solution for this scenario.

Input : input.zip (26.2 KB)

Current OP : Current_Output.zip (26.4 KB)

Exp OP : Exp_Output.zip (26.3 KB)

@priyanga,

For this scenario, please try using the following code:

Document doc = new Document("D:\\Temp\\input\\input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

boolean flag = false;
int i = 0;
for(Paragraph para : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)) {
    // Skip Paragraphs until 'Summary' is reached
    if (!flag) {
        if (!para.toString(SaveFormat.TEXT).trim().equals("Summary"))
            continue;
        else
            flag = true;
    }

    // Skip empty Paragraphs
    if (para.toString(SaveFormat.TEXT).trim().equals(""))
        continue;
    // Skip Keywords Paragraph
    if (para.toString(SaveFormat.TEXT).trim().startsWith("Keywords:"))
        continue;

    builder.moveTo(para);

    BookmarkStart start = builder.startBookmark("bm_" + i);
    BookmarkEnd end = builder.endBookmark("bm_" + i);

    if (para.getChildNodes().getCount() > 2) {
        para.insertBefore(start, para.getFirstChild());
    }

    i++;
}

doc.save("D:\\Temp\\input\\awjava-18.3.docx");

Hi @awais.hafeez

Thank you very much for your quick reply.

Thanks & regards,
Priyanga G