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

Free Support Forum - aspose.com

How to insert bookmark in each paragraph?

Dear Awais,

I request to skip the bookmarks in heading parts from document.

I already done skip the heading parts in documents, when that document headings are applied in style of “Heading” using below condition.

         String value = para.getParagraphFormat().getStyleName();
                       if (value.startsWith("Heading")
                       if (value.startsWith("Heading 1")
                       if (value.startsWith("Heading 2")

This comment working Fine. But Now i need to skip without applied styles in document mean how to skip that headings part ?.

Please give solution for this scenario.


Please share the document and highlight the paragraphs that you want to skip.

Hi Awais,

Please find below documents.

Input : input.zip (118.6 KB)

OP : Exp_O.zip (118.9 KB)

Please skip the bookmark for header (I have mentioned highlighted ) content in document.


The only difference between the normal Paragraphs and Headings is that the headings are Bold and Justified. You can add the following checks to skip them:

if (para.getRuns().getCount()>0 &&
    para.getRuns().get(0).getFont().getBold() == true &&
    para.getParagraphFormat().getAlignment() == ParagraphAlignment.JUSTIFY)

Thank you Awais, Its Working Fine.

Kindly request for another query, how to skip the bookmark “comments” in word document.

Here I have attached sample for comment in document.

Example : Capture.PNG (32.2 KB)

Kindly please give solution for this.


Please share the document and highlight the comments that you want to skip.

Thank you Awais. Here I have attached documents.

Input : input.zip (80.3 KB)

Current OP : output.zip (87.1 KB)

In my current output all comments are bookmarked. How to skip that comments. Please give solution for this one. I have mentioned sample(Comment) in previous post.


Please add the following check. Hope, this helps.

if(para.getAncestor(NodeType.COMMENT) != null)

Thank you Awais, Its very useful for us.

Hi Awais,

I have problem with skip the bookmark from document.

I have used below mention code. In this code start the bookmark in Abstract or introduction part. But i need if document have no abstract or introduction part mean i need to apply bookmark in whole document.

if (!flag) {
if (!para.toString(SaveFormat.TEXT).trim().equals(“Abstract”)) {
if (!para.toString(SaveFormat.TEXT).trim().equals(“Introduction”)) {
} else {
flag = true;
}} else {
flag = true;

Please give solution for this scenario.

Sample document without abstract part : Without abstract.zip (78.4 KB)


Please share your expected document showing the correct bookmarks that you want to insert.

Dear Awais,

I have shared my expected document.

In document without any Abstract or Introduction part mean i have applied bookmark in whole document:

Input : input.zip (78.4 KB)
Expected OP : Exp_Output.zip (59.6 KB)

In document Abstract or introduction para is available mean i have start bookmark with Abstract part.

Input : inp.zip (28.7 KB)
Expected OP : Exp_Output.zip (21.7 KB)


The following code will provide you base to meet your requirements:

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

boolean isAbstract = false;
for(Paragraph para : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)) {
    if (para.toString(SaveFormat.TEXT).trim().equals("Abstract")) {
        isAbstract = true;

if(isAbstract) {
    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("Abstract"))
                flag = true;

        // Skip empty Paragraphs
        if (para.toString(SaveFormat.TEXT).trim().equals(""))
        // Skip Keywords Paragraph
        if (para.toString(SaveFormat.TEXT).trim().startsWith("Keywords:"))
        // Add some more checks etc ...

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


private static void BookmarkParagraph(DocumentBuilder builder, Paragraph para, String bookmarkName){

    BookmarkStart start = builder.startBookmark(bookmarkName);
    BookmarkEnd end = builder.endBookmark(bookmarkName);

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

Hi @awais.hafeez,

Thank you very much.

Dear awais,

Already we are done add bookmark in word document and also we written in text file all the bookmark.It’s working fine.

Now, We need solution for the following concern.
From text File : First We need to read the converted text file. In the text file we need to get the particular bookmark(LEBookMark2) after finding the bookmark in text file
need to find the particular word position(Ex: 618, 625 ) from the bookmark using output text file.

Which we are taken that word from text file that word need to be add comments(Ex:insert comment) in that word in output document.

Output .txt file : Scenario_output.zip (422 Bytes)

Converted .txt file : Scenario_test_input.zip (1.6 KB)

Bookmark Source Doc : Scenario_Bookmark_Output.zip (10.9 KB)

Expected Output : Exp_Output.zip (12.9 KB)

Please kindly give solution for this scenario.


Unfortunately, your query is not clear enough therefore we request you to please elaborate your inquiry further by mentioning the complete details of your use-case. Also, please mention the purpose of each file you shared in your previous post. This will help us to understand your scenario, and we will be in a better position to address your concerns accordingly.

Dear Awais,

I need to insert comments in word document. Using read the text file.

Scenario_output.zip Contains find keywords & comments to be inserted in word document.

Scenario_Bookmark_Output.zip is source document.

Exp_Output.zip is my expected output.

I’m looking to to add up comments in source document by finding keywords.
Ex: Read the text file(Scenario_output.zip) and Insert comments for word “studies” in position of 618 to 625 in source document(Scenario_Bookmark_Output.zip) the comments is there above mentioned in text file(Scenario_output.zip) within message tag.


I believe, you can meet this requirement after reading the following articles:

Thank you. Please send some example for how to insert comment in particular word in document.


The following example shows how to anchor a comment to a region of text [word(s)].

Document doc = new Document();

Paragraph para1 = new Paragraph(doc);
Run run1 = new Run(doc, "Some ");
Run run2 = new Run(doc, "text ");

Paragraph para2 = new Paragraph(doc);
Run run3 = new Run(doc, "is ");
Run run4 = new Run(doc, "added ");

Comment comment = new Comment(doc, "Awais Hafeez", "AH", new Date());
comment.getParagraphs().add(new Paragraph(doc));
comment.getFirstParagraph().getRuns().add(new Run(doc, "Comment text."));

CommentRangeStart commentRangeStart = new CommentRangeStart(doc, comment.getId());
CommentRangeEnd commentRangeEnd = new CommentRangeEnd(doc, comment.getId());

run1.getParentNode().insertAfter(commentRangeStart, run1);
run3.getParentNode().insertAfter(commentRangeEnd, run3);
commentRangeEnd.getParentNode().insertAfter(comment, commentRangeEnd);