com.aspose.words

Class Bookmark

  • java.lang.Object
    • com.aspose.words.Bookmark
public class Bookmark 
extends java.lang.Object

Represents a single bookmark.

Bookmark is a "facade" object that encapsulates two nodes BookmarkStart and BookmarkEnd in a document tree and allows to work with a bookmark as a single object.

Example:

Shows how to get or set bookmark name and text.
Document doc = new Document(getMyDir() + "Bookmark.doc");

// Use the indexer of the Bookmarks collection to obtain the desired bookmark.
Bookmark bookmark = doc.getRange().getBookmarks().get("MyBookmark");

// Get the name and text of the bookmark.
String name = bookmark.getName();
String text = bookmark.getText();

// Set the name and text of the bookmark.
bookmark.setName("RenamedBookmark");
bookmark.setText("This is a new bookmarked text.");

Property Getters/Setters Summary
BookmarkEndgetBookmarkEnd()
Gets the node that represents the end of the bookmark.
BookmarkStartgetBookmarkStart()
Gets the node that represents the start of the bookmark.
java.lang.StringgetName()
void
setName(java.lang.Stringvalue)
           Gets or sets the name of the bookmark.
java.lang.StringgetText()
void
setText(java.lang.Stringvalue)
           Gets or sets the text enclosed in the bookmark.
 
Method Summary
voidremove()
Removes the bookmark from the document. Does not remove text inside the bookmark.
 

    • Property Getters/Setters Detail

      • getBookmarkEnd

        public BookmarkEnd getBookmarkEnd()
        
        Gets the node that represents the end of the bookmark.

        Example:

        Shows how add bookmarks and update their contents.
        public void createUpdateAndPrintBookmarks() throws Exception {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
        
            // Add some bookmarks to the document
            for (int i = 1; i < 4; i++) {
                String bookmarkName = "Bookmark " + i;
        
                builder.startBookmark(bookmarkName);
                builder.write("Text content of " + bookmarkName);
                builder.endBookmark(bookmarkName);
            }
        
            BookmarkCollection bookmarks = doc.getRange().getBookmarks();
        
            // Look at initial values of our bookmarks
            printAllBookmarkInfo(bookmarks);
        
            // Update some values
            bookmarks.get(0).setName("Updated name of " + bookmarks.get(0).getName());
            bookmarks.get(1).setText("Updated text content of " + bookmarks.get(1).getName());
            bookmarks.get(2).remove();
        
            bookmarks = doc.getRange().getBookmarks();
        
            // Look at updated values of our bookmarks
            printAllBookmarkInfo(bookmarks);
        
        }
        
        /// <summary>
        /// Use an iterator and a visitor to print info of every bookmark from within a document.
        /// </summary>
        private static void printAllBookmarkInfo(final BookmarkCollection bookmarks) throws Exception {
            // Create a DocumentVisitor
            BookmarkInfoPrinter bookmarkVisitor = new BookmarkInfoPrinter();
        
            // Get the enumerator from the document's BookmarkCollection and iterate over the bookmarks
            Iterator<Bookmark> enumerator = bookmarks.iterator();
            while (enumerator.hasNext()) {
                Bookmark currentBookmark = enumerator.next();
        
                // Accept our DocumentVisitor it to print information about our bookmarks
                if (currentBookmark != null) {
                    currentBookmark.getBookmarkStart().accept(bookmarkVisitor);
                    currentBookmark.getBookmarkEnd().accept(bookmarkVisitor);
        
                    // Prints a blank line
                    System.out.println(currentBookmark.getBookmarkStart().getText());
                }
            }
        }
        
        /// <summary>
        /// Visitor that prints bookmark information to the console.
        /// </summary>
        public static class BookmarkInfoPrinter extends DocumentVisitor {
            public int visitBookmarkStart(final BookmarkStart bookmarkStart) throws Exception {
                System.out.println(MessageFormat.format("BookmarkStart name: \"{0}\", Content: \"{1}\"", bookmarkStart.getName(),
                        bookmarkStart.getBookmark().getText()));
                return VisitorAction.CONTINUE;
            }
        
            public int visitBookmarkEnd(final BookmarkEnd bookmarkEnd) {
                System.out.println(MessageFormat.format("BookmarkEnd name: \"{0}\"", bookmarkEnd.getName()));
                return VisitorAction.CONTINUE;
            }
        }
      • getBookmarkStart

        public BookmarkStart getBookmarkStart()
        
        Gets the node that represents the start of the bookmark.

        Example:

        Shows how add bookmarks and update their contents.
        public void createUpdateAndPrintBookmarks() throws Exception {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
        
            // Add some bookmarks to the document
            for (int i = 1; i < 4; i++) {
                String bookmarkName = "Bookmark " + i;
        
                builder.startBookmark(bookmarkName);
                builder.write("Text content of " + bookmarkName);
                builder.endBookmark(bookmarkName);
            }
        
            BookmarkCollection bookmarks = doc.getRange().getBookmarks();
        
            // Look at initial values of our bookmarks
            printAllBookmarkInfo(bookmarks);
        
            // Update some values
            bookmarks.get(0).setName("Updated name of " + bookmarks.get(0).getName());
            bookmarks.get(1).setText("Updated text content of " + bookmarks.get(1).getName());
            bookmarks.get(2).remove();
        
            bookmarks = doc.getRange().getBookmarks();
        
            // Look at updated values of our bookmarks
            printAllBookmarkInfo(bookmarks);
        
        }
        
        /// <summary>
        /// Use an iterator and a visitor to print info of every bookmark from within a document.
        /// </summary>
        private static void printAllBookmarkInfo(final BookmarkCollection bookmarks) throws Exception {
            // Create a DocumentVisitor
            BookmarkInfoPrinter bookmarkVisitor = new BookmarkInfoPrinter();
        
            // Get the enumerator from the document's BookmarkCollection and iterate over the bookmarks
            Iterator<Bookmark> enumerator = bookmarks.iterator();
            while (enumerator.hasNext()) {
                Bookmark currentBookmark = enumerator.next();
        
                // Accept our DocumentVisitor it to print information about our bookmarks
                if (currentBookmark != null) {
                    currentBookmark.getBookmarkStart().accept(bookmarkVisitor);
                    currentBookmark.getBookmarkEnd().accept(bookmarkVisitor);
        
                    // Prints a blank line
                    System.out.println(currentBookmark.getBookmarkStart().getText());
                }
            }
        }
        
        /// <summary>
        /// Visitor that prints bookmark information to the console.
        /// </summary>
        public static class BookmarkInfoPrinter extends DocumentVisitor {
            public int visitBookmarkStart(final BookmarkStart bookmarkStart) throws Exception {
                System.out.println(MessageFormat.format("BookmarkStart name: \"{0}\", Content: \"{1}\"", bookmarkStart.getName(),
                        bookmarkStart.getBookmark().getText()));
                return VisitorAction.CONTINUE;
            }
        
            public int visitBookmarkEnd(final BookmarkEnd bookmarkEnd) {
                System.out.println(MessageFormat.format("BookmarkEnd name: \"{0}\"", bookmarkEnd.getName()));
                return VisitorAction.CONTINUE;
            }
        }
      • getName/setName

        public java.lang.String getName() / public void setName(java.lang.String value)
        
        Gets or sets the name of the bookmark. Note that if you change the name of a bookmark to a name that already exists in the document, no error will be given and only the first bookmark will be stored when you save the document.

        Example:

        Shows how to replace elements in bookmark name
        Document doc = new Document(getMyDir() + "Bookmarks.Replace.docx");
        
        // MS Word document does not support bookmark names with whitespaces by default.
        // If you have document which contains bookmark names with underscores, you can simply replace them to whitespaces.
        for (Bookmark bookmark : doc.getRange().getBookmarks()) {
            bookmark.setName(bookmark.getName().replace("_", " "));
        }

        Example:

        Shows how to get or set bookmark name and text.
        Document doc = new Document(getMyDir() + "Bookmark.doc");
        
        // Use the indexer of the Bookmarks collection to obtain the desired bookmark.
        Bookmark bookmark = doc.getRange().getBookmarks().get("MyBookmark");
        
        // Get the name and text of the bookmark.
        String name = bookmark.getName();
        String text = bookmark.getText();
        
        // Set the name and text of the bookmark.
        bookmark.setName("RenamedBookmark");
        bookmark.setText("This is a new bookmarked text.");
      • getText/setText

        public java.lang.String getText() / public void setText(java.lang.String value)
        
        Gets or sets the text enclosed in the bookmark.

        Example:

        Shows how to get or set bookmark name and text.
        Document doc = new Document(getMyDir() + "Bookmark.doc");
        
        // Use the indexer of the Bookmarks collection to obtain the desired bookmark.
        Bookmark bookmark = doc.getRange().getBookmarks().get("MyBookmark");
        
        // Get the name and text of the bookmark.
        String name = bookmark.getName();
        String text = bookmark.getText();
        
        // Set the name and text of the bookmark.
        bookmark.setName("RenamedBookmark");
        bookmark.setText("This is a new bookmarked text.");
    • Method Detail

      • remove

        public void remove()
                   throws java.lang.Exception
        Removes the bookmark from the document. Does not remove text inside the bookmark.

        Example:

        Shows how to remove a particular bookmark from a document.
        Document doc = new Document(getMyDir() + "Bookmark.doc");
        
        // Use the indexer of the Bookmarks collection to obtain the desired bookmark.
        Bookmark bookmark = doc.getRange().getBookmarks().get("MyBookmark");
        
        // Remove the bookmark. The bookmarked text is not deleted.
        bookmark.remove();