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.

Wed Feb 08, 2023 9:20 am

Hi,
We have document with multiple sections.
How I can get in Java section which contain specific bookmark/paragraph?
Thanks Andrei.

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

Thu Feb 09, 2023 6:09 am

Hi,

Thanks for your inquiry.
Please see the following code for reference.
Code: Select all
        Document doc = new Document();
        doc.loadFromFile("test.docx");
        // Creates a BookmarkNavigator instance to access the bookmark
        BookmarksNavigator navigator = new BookmarksNavigator(doc);
        // for example, get the bookmark named "test"
        navigator.moveToBookmark("test");
        // Locate a specific bookmark by bookmark name
        Bookmark currentBookmark = navigator.getCurrentBookmark();
        // get the owner paragraph
        Paragraph ownerParagraph = (Paragraph) currentBookmark.getBookmarkStart().getOwner();
        // get the owner body
        DocumentObject owner = ownerParagraph.getOwner();
        Body body;
        // judge the type of the paragraph's owner
        if (owner.getDocumentObjectType().equals(DocumentObjectType.Table_Cell)){
            TableCell tableCell = (TableCell) owner;
            Table table = (Table) tableCell.getOwnerRow().getOwner();
            body = table.ownerTextBody();
        }else {
            body = ownerParagraph.ownerTextBody();
        }
        // get the owner section
        Section ownerSection = (Section) body.getOwner();

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

Return to Spire.Doc

cron