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

Free Support Forum - aspose.com

Bookmark Paragraph

Dear Team,

I need to find in a document how many paragraphs in word document and each para i need to apply bookmark(paragraph symbol).

Please give solution and advice for this scenario.

@Vadivel_S_S,

Thanks for your inquiry. Please use Document.GetChildNodes method as shown below to get the paragraph count in a document.

Document doc = new Document(MyDir + "in.docx");
NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
System.out.println(paragraphs.getCount());

Please move the cursor to the paragraph and insert the bookmark. Please refer to the following articles.
Inserting a Bookmark
Moving to a Paragraph

Thanks for your quick replying. I used this code but not get perfect result. i have attached sample test document. They have only four paragraph but my output count is six. please clarify.

Input : world.zip (10.0 KB)

And also i need to apply bookmark for each paragraph.

Expected output : Output.zip (10.3 KB)

@Vadivel_S_S,

Thanks for your inquiry.

Your input document has 5 paragraphs and following line of code returns correct value. Please check the attach image for detail. paragraphs.png (30.3 KB)

System.out.println(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());

Please use the following code example to get the desired output. Hope this helps you.

Document doc = new Document(MyDir + "world.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

System.out.println(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
int i = 0;
for(Paragraph para : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)){
    if(para.hasChildNodes())
    {
        BookmarkStart start = new BookmarkStart(doc, "bm_" + i);
        BookmarkEnd end = new BookmarkEnd(doc, "bm_" + i);

        para.insertBefore(start, para.getFirstChild());
        para.insertAfter(end, para.getLastChild());

        i++;
    }
}
doc.save(MyDir + "18.2.docx");

Thanks for your quick reply. I need to convert .docx to .txt file with bookmark. The bookmark replace with text content. I have attached expected output for text file.

Input bookmark doc : bookmark.zip (504.7 KB)

Expected output : expectedoutput.zip (17.4 KB)

@Vadivel_S_S,

Thanks for your inquiry. Please use the following code example to get the desired output. Hope this helps you.

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

for(Bookmark bookmark : doc.getRange().getBookmarks())
{
    if(bookmark.getName().equals("_GoBack"))
        continue;;
   Paragraph para = (Paragraph)bookmark.getBookmarkStart().getParentNode();
    if(para.hasChildNodes())
    {
        Run start = new Run(doc, "<" + bookmark.getName() + ">");
        Run end = new Run(doc, "</" + bookmark.getName()+">");

        para.insertBefore(start, para.getFirstChild());
        para.insertAfter(end, para.getLastChild());
    }

}
doc.save(MyDir + "18.2.txt");

Thank you. its working fine.

Dear tahir,

I have convert bookmark document into text file. Its working fine. Now i need to convert bookmark content only convert into text file. I have attached documents.

Input doc : input.zip (519.3 KB)

Output Doc : Output.zip (504.7 KB)

My Current Output : ConvertedTxtOutput.zip (19.4 KB)

Expected Output : Expected Output.zip (17.5 KB)

please give solution for this scenario.

@Vadivel_S_S,

Thanks for your inquiry. The shared documents “ConvertedTxtOutput.txt” and “Expected Output.txt” are same. Please share your expected output document.

In this case, we suggest you please extract the content from bookmark and save the final document to TXT file format.

Dear Tahir,

Please kindly refer and compare the both documents. There are some difference in both.

In “ConvertedTxtOutput.txt” some unbookmarked table contents are also converted in the output text document. In my requirement bookmarked content only converted into text.

So please kindly provide solution for delete the unbookmarked contents in the converted output.

@Vadivel_S_S,

Thanks for your inquiry. Please use the following code example to get the desired output.

Document doc = new Document(MyDir + "bookmark.docx");
StringBuilder stringBuilder = new StringBuilder();
for(Bookmark bookmark : doc.getRange().getBookmarks())
{
    if(bookmark.getName().equals("_GoBack"))
        continue;;

    Paragraph para = (Paragraph)bookmark.getBookmarkStart().getParentNode();
    if(para.hasChildNodes())
    {
        Run start = new Run(doc, "<" + bookmark.getName() + ">");
        Run end = new Run(doc, "</" + bookmark.getName()+">");

        para.insertAfter(start,bookmark.getBookmarkStart());
        para.insertAfter(end, para.getLastChild());
    }
    stringBuilder.append(bookmark.getText() + System.lineSeparator());
}

System.out.println(stringBuilder.toString());

Thanks tahir, Its working fine. but end tag was missing.

Current output : Content

Expected : Content </End Tag>

@Vadivel_S_S,

Thanks for your inquiry. We have not found this issue at our end. Could you please share the bookmark name for which you are not getting the end tag?

Dear Tahir,

here I have attached my code :new 2.zip (1.4 KB)

In output text file end tag was missed. Please kindly refer and give solution.

@Vadivel_S_S,

Thanks for sharing the detail. Please use the following method to get the desired output. We have attached the output TXT file with this post for your kind reference. outputTXT.zip (18.0 KB)

private static void convertText(String str) throws Exception
{
    Document doc = new Document(str);
    StringBuilder stringBuilder = new StringBuilder();

    for(Bookmark bookmark : doc.getRange().getBookmarks())
    {
        if(bookmark.getName().equals("_GoBack"))
            continue;;
        Paragraph para = (Paragraph)bookmark.getBookmarkStart().getParentNode();
        if(para.hasChildNodes())
        {
            Run start = new Run(doc, "<" + bookmark.getName() + ">");
            Run end = new Run(doc, "</" + bookmark.getName()+">");

            para.insertAfter(start,bookmark.getBookmarkStart());
            para.insertBefore(end, bookmark.getBookmarkEnd());
        }
        stringBuilder.append(bookmark.getText() + System.lineSeparator());

    }

    BufferedWriter bwr = new BufferedWriter(new FileWriter(new File(MyDir + "output.txt")));
    bwr.write(stringBuilder.toString());
    bwr.flush();
    bwr.close();
}

Thank You Tahir.

Hi Tahir,

Request for bookmark problem.If i have already any bookmark in source document its not inserted new bookmark in word document.

My expectation is i need to add existing bookmark with new inserted bookmark. We are used our previous posted code only.

Please give solution for this scenario.

@Vadivel_S_S,

Thanks for your inquiry. Please use Paragraph.Range.Bookmarks to check if a bookmark exists in a paragraph. If bookmark exists in the paragraph, remove it and insert the new one as you are doing in your code.

Thanks Tahir,

Please give some example for this scenario. And i don’t wanna remove existing bookmark i need existing + new bookmark.

Input : inp.zip (2.4 MB)

Expected OP : Insert my new bookmark also with existing bookmark for each para. Its is possible to apply?

@Vadivel_S_S,

Thanks for your inquiry.

Yes, you can insert new bookmark and keep the existing bookmarks.

Please use the same code example to insert the bookmarks. In convertText method, you can ignore the bookmark whose name does not start with “LEBookMark”. Please check the following code snippet.

if(bookmark.getName().equals("_GoBack"))
    continue;
if(!bookmark.getName().startsWith("LEBookMark"))
    continue;