Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Sat Jan 28, 2023 9:59 am

Hi, I trying to get bookmark content with this code:
Code: Select all
    public static void main(String[] args) throws FileNotFoundException {
        Document newDocument = new Document(new FileInputStream(new java.io.File("test_bookmark.docx")), FileFormat.Docx);
        BookmarksNavigator documentBookmarksNavigator = new BookmarksNavigator(newDocument);
        documentBookmarksNavigator.moveToBookmark("test");
        TextBodyPart textBodyPart = documentBookmarksNavigator.getBookmarkContent();
        System.out.println(textBodyPart.getBodyItems());
    }

On documentBookmarksNavigator.getBookmarkContent(); thrown exception
Code: Select all
Exception in thread "main" java.lang.IllegalArgumentException: itemStart and itemEnd must be contained in one text body
   at com.spire.doc.documents.TextBodySelection.<init>(Unknown Source)
   at com.spire.doc.documents.BookmarksNavigator.getBookmarkContent(Unknown Source)


File was created in MS word.

Thanks,
Andrei

andrei.chorin
 
Posts: 68
Joined: Sun Jan 08, 2023 2:36 pm

Sun Jan 29, 2023 6:31 am

Hi,

Thanks for your inquiry.
I analyzed the structure of your document and found that your bookmark end tag was inside of the table, please check the below screenshot.
tags.png

In this case, when you try to get the content between the bookmark start tag and end tag by calling documentBookmarksNavigator.getBookmarkContent() method, this causes the table was not able to be read correctly, so it would throw exception. It can be read when <w:bookmarkEnd w:id="0"/> comes after </w:tbl>. I suggest you select till the next paragraph’s start to make the table tags can be surrounded completely if you want to include a table at the end in a bookmark. Please refer to the below step.
select.png

If you have further questions, just feel free to contact us.

Sincerely,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Sun Jan 29, 2023 7:28 am

Hi Triste.
Thanks for quick answer.
As I said this file produced manually in MS Word and have valid structure.
Do we have any option to read content of bookmark without error?
Thanks,
Andrei

andrei.chorin
 
Posts: 68
Joined: Sun Jan 08, 2023 2:36 pm

Sun Jan 29, 2023 9:51 am

Hi,

Thanks for your feedback.
Please see the code in the attachment for reference.
If the code does not meet your requirement, you can contact us.

Sincerely,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.Doc