BookmarkCollection
Inheritance: java.lang.Object
All Implemented Interfaces: java.lang.Iterable
public class BookmarkCollection implements Iterable
A collection of Bookmark objects that represent the bookmarks in the specified range.
To learn more, visit the Working with Bookmarks documentation article.
Examples:
Shows how to add bookmarks and update their contents.
public void createUpdateAndPrintBookmarks() throws Exception {
// Create a document with three bookmarks, then use a custom document visitor implementation to print their contents.
Document doc = createDocumentWithBookmarks(3);
BookmarkCollection bookmarks = doc.getRange().getBookmarks();
printAllBookmarkInfo(bookmarks);
// Bookmarks can be accessed in the bookmark collection by index or name, and their names can be updated.
bookmarks.get(0).setName("{bookmarks[0].Name}_NewName");
bookmarks.get("MyBookmark_2").setText("Updated text contents of {bookmarks[1].Name}");
// Print all bookmarks again to see updated values.
printAllBookmarkInfo(bookmarks);
}
///
/// Create a document with a given number of bookmarks.
///
private static Document createDocumentWithBookmarks(int numberOfBookmarks) throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
for (int i = 1; i <= numberOfBookmarks; i++) {
String bookmarkName = "MyBookmark_" + i;
builder.write("Text before bookmark.");
builder.startBookmark(bookmarkName);
builder.write(MessageFormat.format("Text inside {0}.", bookmarkName));
builder.endBookmark(bookmarkName);
builder.writeln("Text after bookmark.");
}
return doc;
}
///
/// Use an iterator and a visitor to print info of every bookmark in the collection.
///
private static void printAllBookmarkInfo(BookmarkCollection bookmarks) throws Exception {
BookmarkInfoPrinter bookmarkVisitor = new BookmarkInfoPrinter();
// Get each bookmark in the collection to accept a visitor that will print its contents.
Iterator enumerator = bookmarks.iterator();
while (enumerator.hasNext()) {
Bookmark currentBookmark = enumerator.next();
if (currentBookmark != null) {
currentBookmark.getBookmarkStart().accept(bookmarkVisitor);
currentBookmark.getBookmarkEnd().accept(bookmarkVisitor);
System.out.println(currentBookmark.getBookmarkStart().getText());
}
}
}
///
/// Prints contents of every visited bookmark to the console.
///
public static class BookmarkInfoPrinter extends DocumentVisitor {
public int visitBookmarkStart(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(BookmarkEnd bookmarkEnd) {
System.out.println(MessageFormat.format("BookmarkEnd name: \"{0}\"", bookmarkEnd.getName()));
return VisitorAction.CONTINUE;
}
}
Methods
Method | Description |
---|---|
clear() | Removes all bookmarks from this collection and from the document. |
get(int index) | Returns a bookmark at the specified index. |
get(String bookmarkName) | Returns a bookmark by name. |
getCount() | Returns the number of bookmarks in the collection. |
iterator() | Returns an enumerator object. |
remove(Bookmark bookmark) | Removes the specified bookmark from the document. |
remove(String bookmarkName) | Removes a bookmark with the specified name. |
removeAt(int index) | Removes a bookmark at the specified index. |
clear()
public void clear()
Removes all bookmarks from this collection and from the document.
Examples:
Shows how to remove bookmarks from a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert five bookmarks with text inside their boundaries.
for (int i = 1; i <= 5; i++) {
String bookmarkName = "MyBookmark_" + i;
builder.startBookmark(bookmarkName);
builder.write(MessageFormat.format("Text inside {0}.", bookmarkName));
builder.endBookmark(bookmarkName);
builder.insertBreak(BreakType.PARAGRAPH_BREAK);
}
// This collection stores bookmarks.
BookmarkCollection bookmarks = doc.getRange().getBookmarks();
Assert.assertEquals(5, bookmarks.getCount());
// There are several ways of removing bookmarks.
// 1 - Calling the bookmark's Remove method:
bookmarks.get("MyBookmark_1").remove();
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_1"));
// 2 - Passing the bookmark to the collection's Remove method:
Bookmark bookmark = doc.getRange().getBookmarks().get(0);
doc.getRange().getBookmarks().remove(bookmark);
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_2"));
// 3 - Removing a bookmark from the collection by name:
doc.getRange().getBookmarks().remove("MyBookmark_3");
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_3"));
// 4 - Removing a bookmark at an index in the bookmark collection:
doc.getRange().getBookmarks().removeAt(0);
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_4"));
// We can clear the entire bookmark collection.
bookmarks.clear();
// The text that was inside the bookmarks is still present in the document.
Assert.assertEquals(bookmarks.getCount(), 0);
Assert.assertEquals("Text inside MyBookmark_1.\r" +
"Text inside MyBookmark_2.\r" +
"Text inside MyBookmark_3.\r" +
"Text inside MyBookmark_4.\r" +
"Text inside MyBookmark_5.", doc.getText().trim());
get(int index)
public Bookmark get(int index)
Returns a bookmark at the specified index.
Remarks:
The index is zero-based.
Negative indexes are allowed and indicate access from the back of the collection. For example -1 means the last item, -2 means the second before last and so on.
If index is greater than or equal to the number of items in the list, this returns a null reference.
If index is negative and its absolute value is greater than the number of items in the list, this returns a null reference.
Examples:
Shows how to add bookmarks and update their contents.
public void createUpdateAndPrintBookmarks() throws Exception {
// Create a document with three bookmarks, then use a custom document visitor implementation to print their contents.
Document doc = createDocumentWithBookmarks(3);
BookmarkCollection bookmarks = doc.getRange().getBookmarks();
printAllBookmarkInfo(bookmarks);
// Bookmarks can be accessed in the bookmark collection by index or name, and their names can be updated.
bookmarks.get(0).setName("{bookmarks[0].Name}_NewName");
bookmarks.get("MyBookmark_2").setText("Updated text contents of {bookmarks[1].Name}");
// Print all bookmarks again to see updated values.
printAllBookmarkInfo(bookmarks);
}
///
/// Create a document with a given number of bookmarks.
///
private static Document createDocumentWithBookmarks(int numberOfBookmarks) throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
for (int i = 1; i <= numberOfBookmarks; i++) {
String bookmarkName = "MyBookmark_" + i;
builder.write("Text before bookmark.");
builder.startBookmark(bookmarkName);
builder.write(MessageFormat.format("Text inside {0}.", bookmarkName));
builder.endBookmark(bookmarkName);
builder.writeln("Text after bookmark.");
}
return doc;
}
///
/// Use an iterator and a visitor to print info of every bookmark in the collection.
///
private static void printAllBookmarkInfo(BookmarkCollection bookmarks) throws Exception {
BookmarkInfoPrinter bookmarkVisitor = new BookmarkInfoPrinter();
// Get each bookmark in the collection to accept a visitor that will print its contents.
Iterator enumerator = bookmarks.iterator();
while (enumerator.hasNext()) {
Bookmark currentBookmark = enumerator.next();
if (currentBookmark != null) {
currentBookmark.getBookmarkStart().accept(bookmarkVisitor);
currentBookmark.getBookmarkEnd().accept(bookmarkVisitor);
System.out.println(currentBookmark.getBookmarkStart().getText());
}
}
}
///
/// Prints contents of every visited bookmark to the console.
///
public static class BookmarkInfoPrinter extends DocumentVisitor {
public int visitBookmarkStart(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(BookmarkEnd bookmarkEnd) {
System.out.println(MessageFormat.format("BookmarkEnd name: \"{0}\"", bookmarkEnd.getName()));
return VisitorAction.CONTINUE;
}
}
Parameters:
Parameter | Type | Description |
---|---|---|
index | int | An index into the collection. |
Returns: Bookmark - A bookmark at the specified index.
get(String bookmarkName)
public Bookmark get(String bookmarkName)
Returns a bookmark by name.
Remarks:
Returns null if the bookmark with the specified name cannot be found.
Examples:
Shows how to add bookmarks and update their contents.
public void createUpdateAndPrintBookmarks() throws Exception {
// Create a document with three bookmarks, then use a custom document visitor implementation to print their contents.
Document doc = createDocumentWithBookmarks(3);
BookmarkCollection bookmarks = doc.getRange().getBookmarks();
printAllBookmarkInfo(bookmarks);
// Bookmarks can be accessed in the bookmark collection by index or name, and their names can be updated.
bookmarks.get(0).setName("{bookmarks[0].Name}_NewName");
bookmarks.get("MyBookmark_2").setText("Updated text contents of {bookmarks[1].Name}");
// Print all bookmarks again to see updated values.
printAllBookmarkInfo(bookmarks);
}
///
/// Create a document with a given number of bookmarks.
///
private static Document createDocumentWithBookmarks(int numberOfBookmarks) throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
for (int i = 1; i <= numberOfBookmarks; i++) {
String bookmarkName = "MyBookmark_" + i;
builder.write("Text before bookmark.");
builder.startBookmark(bookmarkName);
builder.write(MessageFormat.format("Text inside {0}.", bookmarkName));
builder.endBookmark(bookmarkName);
builder.writeln("Text after bookmark.");
}
return doc;
}
///
/// Use an iterator and a visitor to print info of every bookmark in the collection.
///
private static void printAllBookmarkInfo(BookmarkCollection bookmarks) throws Exception {
BookmarkInfoPrinter bookmarkVisitor = new BookmarkInfoPrinter();
// Get each bookmark in the collection to accept a visitor that will print its contents.
Iterator enumerator = bookmarks.iterator();
while (enumerator.hasNext()) {
Bookmark currentBookmark = enumerator.next();
if (currentBookmark != null) {
currentBookmark.getBookmarkStart().accept(bookmarkVisitor);
currentBookmark.getBookmarkEnd().accept(bookmarkVisitor);
System.out.println(currentBookmark.getBookmarkStart().getText());
}
}
}
///
/// Prints contents of every visited bookmark to the console.
///
public static class BookmarkInfoPrinter extends DocumentVisitor {
public int visitBookmarkStart(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(BookmarkEnd bookmarkEnd) {
System.out.println(MessageFormat.format("BookmarkEnd name: \"{0}\"", bookmarkEnd.getName()));
return VisitorAction.CONTINUE;
}
}
Parameters:
Parameter | Type | Description |
---|---|---|
bookmarkName | java.lang.String | Case-insensitive name of the bookmark. |
Returns: Bookmark - A bookmark by name.
getCount()
public int getCount()
Returns the number of bookmarks in the collection.
Examples:
Shows how to remove bookmarks from a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert five bookmarks with text inside their boundaries.
for (int i = 1; i <= 5; i++) {
String bookmarkName = "MyBookmark_" + i;
builder.startBookmark(bookmarkName);
builder.write(MessageFormat.format("Text inside {0}.", bookmarkName));
builder.endBookmark(bookmarkName);
builder.insertBreak(BreakType.PARAGRAPH_BREAK);
}
// This collection stores bookmarks.
BookmarkCollection bookmarks = doc.getRange().getBookmarks();
Assert.assertEquals(5, bookmarks.getCount());
// There are several ways of removing bookmarks.
// 1 - Calling the bookmark's Remove method:
bookmarks.get("MyBookmark_1").remove();
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_1"));
// 2 - Passing the bookmark to the collection's Remove method:
Bookmark bookmark = doc.getRange().getBookmarks().get(0);
doc.getRange().getBookmarks().remove(bookmark);
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_2"));
// 3 - Removing a bookmark from the collection by name:
doc.getRange().getBookmarks().remove("MyBookmark_3");
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_3"));
// 4 - Removing a bookmark at an index in the bookmark collection:
doc.getRange().getBookmarks().removeAt(0);
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_4"));
// We can clear the entire bookmark collection.
bookmarks.clear();
// The text that was inside the bookmarks is still present in the document.
Assert.assertEquals(bookmarks.getCount(), 0);
Assert.assertEquals("Text inside MyBookmark_1.\r" +
"Text inside MyBookmark_2.\r" +
"Text inside MyBookmark_3.\r" +
"Text inside MyBookmark_4.\r" +
"Text inside MyBookmark_5.", doc.getText().trim());
Returns: int - The number of bookmarks in the collection.
iterator()
public Iterator iterator()
Returns an enumerator object.
Examples:
Shows how to add bookmarks and update their contents.
public void createUpdateAndPrintBookmarks() throws Exception {
// Create a document with three bookmarks, then use a custom document visitor implementation to print their contents.
Document doc = createDocumentWithBookmarks(3);
BookmarkCollection bookmarks = doc.getRange().getBookmarks();
printAllBookmarkInfo(bookmarks);
// Bookmarks can be accessed in the bookmark collection by index or name, and their names can be updated.
bookmarks.get(0).setName("{bookmarks[0].Name}_NewName");
bookmarks.get("MyBookmark_2").setText("Updated text contents of {bookmarks[1].Name}");
// Print all bookmarks again to see updated values.
printAllBookmarkInfo(bookmarks);
}
///
/// Create a document with a given number of bookmarks.
///
private static Document createDocumentWithBookmarks(int numberOfBookmarks) throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
for (int i = 1; i <= numberOfBookmarks; i++) {
String bookmarkName = "MyBookmark_" + i;
builder.write("Text before bookmark.");
builder.startBookmark(bookmarkName);
builder.write(MessageFormat.format("Text inside {0}.", bookmarkName));
builder.endBookmark(bookmarkName);
builder.writeln("Text after bookmark.");
}
return doc;
}
///
/// Use an iterator and a visitor to print info of every bookmark in the collection.
///
private static void printAllBookmarkInfo(BookmarkCollection bookmarks) throws Exception {
BookmarkInfoPrinter bookmarkVisitor = new BookmarkInfoPrinter();
// Get each bookmark in the collection to accept a visitor that will print its contents.
Iterator enumerator = bookmarks.iterator();
while (enumerator.hasNext()) {
Bookmark currentBookmark = enumerator.next();
if (currentBookmark != null) {
currentBookmark.getBookmarkStart().accept(bookmarkVisitor);
currentBookmark.getBookmarkEnd().accept(bookmarkVisitor);
System.out.println(currentBookmark.getBookmarkStart().getText());
}
}
}
///
/// Prints contents of every visited bookmark to the console.
///
public static class BookmarkInfoPrinter extends DocumentVisitor {
public int visitBookmarkStart(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(BookmarkEnd bookmarkEnd) {
System.out.println(MessageFormat.format("BookmarkEnd name: \"{0}\"", bookmarkEnd.getName()));
return VisitorAction.CONTINUE;
}
}
Returns: java.util.Iterator
remove(Bookmark bookmark)
public void remove(Bookmark bookmark)
Removes the specified bookmark from the document.
Examples:
Shows how to remove bookmarks from a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert five bookmarks with text inside their boundaries.
for (int i = 1; i <= 5; i++) {
String bookmarkName = "MyBookmark_" + i;
builder.startBookmark(bookmarkName);
builder.write(MessageFormat.format("Text inside {0}.", bookmarkName));
builder.endBookmark(bookmarkName);
builder.insertBreak(BreakType.PARAGRAPH_BREAK);
}
// This collection stores bookmarks.
BookmarkCollection bookmarks = doc.getRange().getBookmarks();
Assert.assertEquals(5, bookmarks.getCount());
// There are several ways of removing bookmarks.
// 1 - Calling the bookmark's Remove method:
bookmarks.get("MyBookmark_1").remove();
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_1"));
// 2 - Passing the bookmark to the collection's Remove method:
Bookmark bookmark = doc.getRange().getBookmarks().get(0);
doc.getRange().getBookmarks().remove(bookmark);
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_2"));
// 3 - Removing a bookmark from the collection by name:
doc.getRange().getBookmarks().remove("MyBookmark_3");
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_3"));
// 4 - Removing a bookmark at an index in the bookmark collection:
doc.getRange().getBookmarks().removeAt(0);
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_4"));
// We can clear the entire bookmark collection.
bookmarks.clear();
// The text that was inside the bookmarks is still present in the document.
Assert.assertEquals(bookmarks.getCount(), 0);
Assert.assertEquals("Text inside MyBookmark_1.\r" +
"Text inside MyBookmark_2.\r" +
"Text inside MyBookmark_3.\r" +
"Text inside MyBookmark_4.\r" +
"Text inside MyBookmark_5.", doc.getText().trim());
Parameters:
Parameter | Type | Description |
---|---|---|
bookmark | Bookmark | The bookmark to remove. |
remove(String bookmarkName)
public void remove(String bookmarkName)
Removes a bookmark with the specified name.
Examples:
Shows how to remove bookmarks from a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert five bookmarks with text inside their boundaries.
for (int i = 1; i <= 5; i++) {
String bookmarkName = "MyBookmark_" + i;
builder.startBookmark(bookmarkName);
builder.write(MessageFormat.format("Text inside {0}.", bookmarkName));
builder.endBookmark(bookmarkName);
builder.insertBreak(BreakType.PARAGRAPH_BREAK);
}
// This collection stores bookmarks.
BookmarkCollection bookmarks = doc.getRange().getBookmarks();
Assert.assertEquals(5, bookmarks.getCount());
// There are several ways of removing bookmarks.
// 1 - Calling the bookmark's Remove method:
bookmarks.get("MyBookmark_1").remove();
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_1"));
// 2 - Passing the bookmark to the collection's Remove method:
Bookmark bookmark = doc.getRange().getBookmarks().get(0);
doc.getRange().getBookmarks().remove(bookmark);
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_2"));
// 3 - Removing a bookmark from the collection by name:
doc.getRange().getBookmarks().remove("MyBookmark_3");
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_3"));
// 4 - Removing a bookmark at an index in the bookmark collection:
doc.getRange().getBookmarks().removeAt(0);
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_4"));
// We can clear the entire bookmark collection.
bookmarks.clear();
// The text that was inside the bookmarks is still present in the document.
Assert.assertEquals(bookmarks.getCount(), 0);
Assert.assertEquals("Text inside MyBookmark_1.\r" +
"Text inside MyBookmark_2.\r" +
"Text inside MyBookmark_3.\r" +
"Text inside MyBookmark_4.\r" +
"Text inside MyBookmark_5.", doc.getText().trim());
Parameters:
Parameter | Type | Description |
---|---|---|
bookmarkName | java.lang.String | The case-insensitive name of the bookmark to remove. |
removeAt(int index)
public void removeAt(int index)
Removes a bookmark at the specified index.
Examples:
Shows how to remove bookmarks from a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert five bookmarks with text inside their boundaries.
for (int i = 1; i <= 5; i++) {
String bookmarkName = "MyBookmark_" + i;
builder.startBookmark(bookmarkName);
builder.write(MessageFormat.format("Text inside {0}.", bookmarkName));
builder.endBookmark(bookmarkName);
builder.insertBreak(BreakType.PARAGRAPH_BREAK);
}
// This collection stores bookmarks.
BookmarkCollection bookmarks = doc.getRange().getBookmarks();
Assert.assertEquals(5, bookmarks.getCount());
// There are several ways of removing bookmarks.
// 1 - Calling the bookmark's Remove method:
bookmarks.get("MyBookmark_1").remove();
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_1"));
// 2 - Passing the bookmark to the collection's Remove method:
Bookmark bookmark = doc.getRange().getBookmarks().get(0);
doc.getRange().getBookmarks().remove(bookmark);
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_2"));
// 3 - Removing a bookmark from the collection by name:
doc.getRange().getBookmarks().remove("MyBookmark_3");
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_3"));
// 4 - Removing a bookmark at an index in the bookmark collection:
doc.getRange().getBookmarks().removeAt(0);
Assert.assertFalse(IterableUtils.matchesAny(bookmarks, b -> b.getName() == "MyBookmark_4"));
// We can clear the entire bookmark collection.
bookmarks.clear();
// The text that was inside the bookmarks is still present in the document.
Assert.assertEquals(bookmarks.getCount(), 0);
Assert.assertEquals("Text inside MyBookmark_1.\r" +
"Text inside MyBookmark_2.\r" +
"Text inside MyBookmark_3.\r" +
"Text inside MyBookmark_4.\r" +
"Text inside MyBookmark_5.", doc.getText().trim());
Parameters:
Parameter | Type | Description |
---|---|---|
index | int | The zero-based index of the bookmark to remove. |