java.lang.Object
com.aspose.words.BookmarkCollection
public class BookmarkCollection
To learn more, visit the Working with Bookmarks documentation article. Example:
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);
}
/// <summary>
/// Create a document with a given number of bookmarks.
/// </summary>
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;
}
/// <summary>
/// Use an iterator and a visitor to print info of every bookmark in the collection.
/// </summary>
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<Bookmark> 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());
}
}
}
/// <summary>
/// Prints contents of every visited bookmark to the console.
/// </summary>
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;
}
}
| Property Getters/Setters Summary | ||
|---|---|---|
int | getCount() | |
| Returns the number of bookmarks in the collection. | ||
Bookmark | get(int index) | |
| Returns a bookmark at the specified index. | ||
Bookmark | get(java.lang.String bookmarkName) | |
| Returns a bookmark by name. | ||
| Method Summary | ||
|---|---|---|
void | clear() | |
| Removes all bookmarks from this collection and from the document. | ||
java.util.Iterator<Bookmark> | iterator() | |
| Returns an enumerator object. | ||
void | remove(Bookmark bookmark) | |
| Removes the specified bookmark from the document. | ||
void | remove(java.lang.String bookmarkName) | |
| Removes a bookmark with the specified name. | ||
void | removeAt(int index) | |
| Removes a bookmark at the specified index. | ||
| Property Getters/Setters Detail |
|---|
getCount | |
public int getCount() | |
Example:
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 | |
public Bookmark get(int index) | |
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.
index - An index into the collection.Example:
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);
}
/// <summary>
/// Create a document with a given number of bookmarks.
/// </summary>
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;
}
/// <summary>
/// Use an iterator and a visitor to print info of every bookmark in the collection.
/// </summary>
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<Bookmark> 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());
}
}
}
/// <summary>
/// Prints contents of every visited bookmark to the console.
/// </summary>
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;
}
}get | |
public Bookmark get(java.lang.String bookmarkName) | |
Returns null if the bookmark with the specified name cannot be found.
bookmarkName - Case-insensitive name of the bookmark.Example:
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);
}
/// <summary>
/// Create a document with a given number of bookmarks.
/// </summary>
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;
}
/// <summary>
/// Use an iterator and a visitor to print info of every bookmark in the collection.
/// </summary>
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<Bookmark> 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());
}
}
}
/// <summary>
/// Prints contents of every visited bookmark to the console.
/// </summary>
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;
}
}| Method Detail |
|---|
clear | |
public void clear()
throws java.lang.Exception | |
Example:
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());iterator | |
public java.util.Iterator<Bookmark> iterator() | |
Example:
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);
}
/// <summary>
/// Create a document with a given number of bookmarks.
/// </summary>
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;
}
/// <summary>
/// Use an iterator and a visitor to print info of every bookmark in the collection.
/// </summary>
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<Bookmark> 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());
}
}
}
/// <summary>
/// Prints contents of every visited bookmark to the console.
/// </summary>
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;
}
}remove | |
public void remove(Bookmark bookmark) throws java.lang.Exception | |
bookmark - The bookmark to remove.Example:
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());remove | |
public void remove(java.lang.String bookmarkName)
throws java.lang.Exception | |
bookmarkName - The case-insensitive name of the bookmark to remove.Example:
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());removeAt | |
public void removeAt(int index)
throws java.lang.Exception | |
index - The zero-based index of the bookmark to remove.Example:
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());