Java Operation Word Bookmarks: Add, Delete, Read Bookmarks

Keywords: Java Maven

In Word, the bookmark function is often used to find, locate and mark specific characters or paragraphs. It is very practical for large documents. Next, we will introduce how to add and delete Word bookmarks through Java programs. Examples include:

1. Adding bookmarks

1.1 Bookmark the specified paragraph

1.2 Bookmark the specified string

2. Delete bookmarks

2.1 Delete bookmarks

2.2 Delete bookmark text

3. Reading Bookmark Text

Use Tools: Free Spire.Doc for Java (Free Edition)

Jar file acquisition and import:

Method 1: Through the official website Download jar File package. After downloading, extract the file. And import Spire.Doc.jar file under lib folder into java program. Refer to the following import effect:

Method 2: Through maven warehouse Installation import . Reference Installation Import method.

 

Java code example

[Example 1] Bookmark a specified paragraph

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;

public class AppendBookmark {
    public static void main(String[]args){
        //Loading Bookmarks Word File
        Document doc = new Document();
        doc.loadFromFile("sample.docx");

        //Get the paragraph that needs to be bookmarked
        Paragraph para = doc.getSections().get(0).getParagraphs().get(1);

        //Add the start tag and end tag of the bookmark at the beginning and end of the paragraph, and name the bookmark
        BookmarkStart start = para.appendBookmarkStart("bookmark01");
        para.getItems().insert(0,start);
        para.appendBookmarkEnd("bookmark01");

        //Save document
        doc.saveToFile("appendbookmark.docx",FileFormat.Docx_2013);
        doc.dispose();
    }
}

Bookmark add effect:

 

[Example 2] Bookmark the specified string

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.TextRange;

public class AppendBookmarkToCharacter {
    public static void main(String[]args){
        //Loading documents
        Document doc = new Document();
        doc.loadFromFile("sample.docx");

        //Find the specified string
        TextSelection textSelection = doc.findString("It uses symbolic language with deep meaning to reveal the philosophy of life.",false,false);
        TextRange range = textSelection.getAsOneRange();
        Paragraph para = range.getOwnerParagraph();
        int index = para.getChildObjects().indexOf(range);

        //add bookmark
        BookmarkStart start = new BookmarkStart(doc,"Bookmark 1");
        BookmarkEnd end = new BookmarkEnd(doc, "Bookmark 1");
        para.getChildObjects().insert(index, start);
        para.getChildObjects().insert(index + 2, end);

        //Save document
        doc.saveToFile("appendbookmarktocharacter.docx",FileFormat.Docx_2013);
        doc.dispose();
    }
}

Bookmark add effect:

 

[Example 3] Delete bookmarks, bookmark text

 

import com.spire.doc.*;
import com.spire.doc.documents.BookmarksNavigator;

public class DeleteBookmarkAndBookmarkcontent {
    public static void main(String[]args){
        //Loading documents
        Document doc = new Document();
        doc.loadFromFile("test.docx");

        //Locate specific bookmarks
        BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(doc);
        bookmarksNavigator.moveToBookmark("bookmark1");

        //Delete the content at the bookmark
        bookmarksNavigator.deleteBookmarkContent(true);

        //Delete bookmarks (only delete bookmark labels, the contents of the original signature)
        doc.getBookmarks().remove(doc.getBookmarks().get("bookmark1"));//Delete by book signature
        doc.getBookmarks().removeAt(0);//Delete by index value

        //Save document
        doc.saveToFile("deletebookmark.docx",FileFormat.Docx_2013);
    }
}

[Example 4] Read bookmark text

 

import com.spire.doc.*;
import com.spire.doc.documents.BookmarksNavigator;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextBodyPart;
import com.spire.doc.fields.TextRange;

import java.io.IOException;
import java.io.PrintWriter;

public class GetBookmarkText {
    public static void main(String[]args) throws IOException {
        //Loading bookmarks Word File
        Document doc = new Document();
        doc.loadFromFile("test.docx");

        //Get bookmarks
        BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(doc);
        bookmarksNavigator.moveToBookmark("bookmark1");

        //Getting Bookmark Text
        TextBodyPart textBodyPart = bookmarksNavigator.getBookmarkContent();

        //Establish String variable
        String text = "";

        //Items that traverse bookmark content
        for (Object item : textBodyPart.getBodyItems()) {

            //Determine whether the item is a paragraph
            if (item instanceof Paragraph) {
                Paragraph paragraph = (Paragraph) item;

                //Traversing subobjects in paragraphs
                for (Object childObj : paragraph.getChildObjects()) {

                    //Judging whether a child object is TextRange
                    if (childObj instanceof TextRange) {

                        //Obtain TextRange Text in
                        TextRange textRange = (TextRange) childObj;
                        text = text + textRange.getText();
                    }
                }
            }
        }

        //Write the retrieved text Txt file
        PrintWriter printWriter = new PrintWriter("BookmarkText.txt");
        printWriter.println(text);
        printWriter.close();
    }
}

Bookmark reading results:

 

(End of this article)

Posted by Emir on Sat, 05 Oct 2019 05:29:30 -0700