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.

Thu Apr 23, 2026 7:31 am

Hello support team,

I've the following test document which contains a simple table and two bookmarks. The 1st bookmark encloses row 1 and 2 and the 2nd bookmark encloses row 3 and 4.

Now have a look on the following simple test code:
Code: Select all
    public static void main(String[] args) {
        Document document = new Document();
        document.loadFromFile("Test.docx");
        Bookmark bookmark1 = document.getBookmarks().get("B1");
        int rowIndexStart1 = getTableCell(bookmark1.getBookmarkStart()).getOwnerRow().getRowIndex();
        int rowIndexEnd1 = getTableCell(bookmark1.getBookmarkEnd()).getOwnerRow().getRowIndex();
        System.out.println("Bookmark 1 encloses rows " + rowIndexStart1 + " to " + rowIndexEnd1);
        Bookmark bookmark2 = document.getBookmarks().get("B2");
        int rowIndexStart2 = getTableCell(bookmark2.getBookmarkStart()).getOwnerRow().getRowIndex();
        int rowIndexEnd2 = getTableCell(bookmark2.getBookmarkEnd()).getOwnerRow().getRowIndex();
        System.out.println("Bookmark 2 encloses rows " + rowIndexStart2 + " to " + rowIndexEnd2);
    }

    public static TableCell getTableCell(final DocumentObject documentObject) {
        DocumentObject parent = documentObject.getOwner();
        while (parent != null && !(parent instanceof TableCell)) {
            parent = parent.getOwner();
        }
        return (TableCell) parent;
    }


I expect the following output:
Bookmark 1 encloses rows 0 to 1
Bookmark 2 encloses rows 2 to 3


but I actually get the following output:
Bookmark 1 encloses rows 0 to 2
Bookmark 2 encloses rows 2 to 3


So the bookmarks seem overlapping but they aren't. I consider this as a bug and ask you to have a closer look to it.

Kind regards,
Marcus

mgattinger
 
Posts: 65
Joined: Fri May 21, 2021 9:03 am

Thu Apr 23, 2026 9:39 am

I think I found the reason for the overlapping. I seems like Word itself is responsible for it as a close look into the markup reveals:

OpenXml.png


The end marker of the 1st bookmark is placed within the 3rd table row.
Very strange this behavior of Word. So not an issue in your library.

Any idea how to prevent this behavior in Word, i.e. that the bookmark end marker is placed within the 2nd table row?

Kind regards,
Marcus
Last edited by mgattinger on Thu Apr 23, 2026 11:59 am, edited 1 time in total.

mgattinger
 
Posts: 65
Joined: Fri May 21, 2021 9:03 am

Thu Apr 23, 2026 10:19 am

Hello,

Thank you for your letter,
Yes, after adding the bookmarks using Microsoft Word, its underlying XML data is consistent with what we currently obtain from our product. We read it based on XML data parsing, this is difficult to control.

If you have a template document, you can add bookmark start and end tags to your document through our product, such as defining start and end elements, and then adding them at the corresponding paragraph positions.
Code: Select all
 BookmarkStart start = new BookmarkStart(document, "B1");
        BookmarkEnd end = new BookmarkEnd(document, "B1");

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1536
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.Doc

cron