java.lang.Objectcom.aspose.words.BookmarkCollection
public class BookmarkCollection
Example:
public void createUpdateAndPrintBookmarks() throws Exception {
// Create a document with 3 bookmarks: "MyBookmark 1", "MyBookmark 2", "MyBookmark 3"
Document doc = createDocumentWithBookmarks();
BookmarkCollection bookmarks = doc.getRange().getBookmarks();
// Look at initial values of our bookmarks
printAllBookmarkInfo(bookmarks);
// Obtain bookmarks from a bookmark collection by index/name and update their values
bookmarks.get(0).setName("Updated name of " + bookmarks.get(0).getName());
bookmarks.get("MyBookmark 2").setText("Updated text content of " + bookmarks.get(1).getName());
// Remove the latest bookmark
// The bookmarked text is not deleted
bookmarks.get(2).remove();
bookmarks = doc.getRange().getBookmarks();
// Check that we have 2 bookmarks after the latest bookmark was deleted
Assert.assertEquals(bookmarks.getCount(), 2);
// Look at updated values of our bookmarks
printAllBookmarkInfo(bookmarks);
}
/// <summary>
/// Create a document with bookmarks using the start and end nodes.
/// </summary>
private static Document createDocumentWithBookmarks() throws Exception {
DocumentBuilder builder = new DocumentBuilder();
Document doc = builder.getDocument();
// An empty document has just one empty paragraph by default
Paragraph p = doc.getFirstSection().getBody().getFirstParagraph();
// Add several bookmarks to the document
for (int i = 1; i <= 3; i++) {
String bookmarkName = "MyBookmark " + i;
p.appendChild(new Run(doc, "Text before bookmark."));
p.appendChild(new BookmarkStart(doc, bookmarkName));
p.appendChild(new Run(doc, "Text content of " + bookmarkName));
p.appendChild(new BookmarkEnd(doc, bookmarkName));
p.appendChild(new Run(doc, "Text after bookmark.\r\n"));
}
return builder.getDocument();
}
/// <summary>
/// Use an iterator and a visitor to print info of every bookmark from within a document.
/// </summary>
private static void printAllBookmarkInfo(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(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 add bookmarks and update their contents.public void createUpdateAndPrintBookmarks() throws Exception { // Create a document with 3 bookmarks: "MyBookmark 1", "MyBookmark 2", "MyBookmark 3" Document doc = createDocumentWithBookmarks(); BookmarkCollection bookmarks = doc.getRange().getBookmarks(); // Look at initial values of our bookmarks printAllBookmarkInfo(bookmarks); // Obtain bookmarks from a bookmark collection by index/name and update their values bookmarks.get(0).setName("Updated name of " + bookmarks.get(0).getName()); bookmarks.get("MyBookmark 2").setText("Updated text content of " + bookmarks.get(1).getName()); // Remove the latest bookmark // The bookmarked text is not deleted bookmarks.get(2).remove(); bookmarks = doc.getRange().getBookmarks(); // Check that we have 2 bookmarks after the latest bookmark was deleted Assert.assertEquals(bookmarks.getCount(), 2); // Look at updated values of our bookmarks printAllBookmarkInfo(bookmarks); } /// <summary> /// Create a document with bookmarks using the start and end nodes. /// </summary> private static Document createDocumentWithBookmarks() throws Exception { DocumentBuilder builder = new DocumentBuilder(); Document doc = builder.getDocument(); // An empty document has just one empty paragraph by default Paragraph p = doc.getFirstSection().getBody().getFirstParagraph(); // Add several bookmarks to the document for (int i = 1; i <= 3; i++) { String bookmarkName = "MyBookmark " + i; p.appendChild(new Run(doc, "Text before bookmark.")); p.appendChild(new BookmarkStart(doc, bookmarkName)); p.appendChild(new Run(doc, "Text content of " + bookmarkName)); p.appendChild(new BookmarkEnd(doc, bookmarkName)); p.appendChild(new Run(doc, "Text after bookmark.\r\n")); } return builder.getDocument(); } /// <summary> /// Use an iterator and a visitor to print info of every bookmark from within a document. /// </summary> private static void printAllBookmarkInfo(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(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(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 3 bookmarks: "MyBookmark 1", "MyBookmark 2", "MyBookmark 3" Document doc = createDocumentWithBookmarks(); BookmarkCollection bookmarks = doc.getRange().getBookmarks(); // Look at initial values of our bookmarks printAllBookmarkInfo(bookmarks); // Obtain bookmarks from a bookmark collection by index/name and update their values bookmarks.get(0).setName("Updated name of " + bookmarks.get(0).getName()); bookmarks.get("MyBookmark 2").setText("Updated text content of " + bookmarks.get(1).getName()); // Remove the latest bookmark // The bookmarked text is not deleted bookmarks.get(2).remove(); bookmarks = doc.getRange().getBookmarks(); // Check that we have 2 bookmarks after the latest bookmark was deleted Assert.assertEquals(bookmarks.getCount(), 2); // Look at updated values of our bookmarks printAllBookmarkInfo(bookmarks); } /// <summary> /// Create a document with bookmarks using the start and end nodes. /// </summary> private static Document createDocumentWithBookmarks() throws Exception { DocumentBuilder builder = new DocumentBuilder(); Document doc = builder.getDocument(); // An empty document has just one empty paragraph by default Paragraph p = doc.getFirstSection().getBody().getFirstParagraph(); // Add several bookmarks to the document for (int i = 1; i <= 3; i++) { String bookmarkName = "MyBookmark " + i; p.appendChild(new Run(doc, "Text before bookmark.")); p.appendChild(new BookmarkStart(doc, bookmarkName)); p.appendChild(new Run(doc, "Text content of " + bookmarkName)); p.appendChild(new BookmarkEnd(doc, bookmarkName)); p.appendChild(new Run(doc, "Text after bookmark.\r\n")); } return builder.getDocument(); } /// <summary> /// Use an iterator and a visitor to print info of every bookmark from within a document. /// </summary> private static void printAllBookmarkInfo(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(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 3 bookmarks: "MyBookmark 1", "MyBookmark 2", "MyBookmark 3" Document doc = createDocumentWithBookmarks(); BookmarkCollection bookmarks = doc.getRange().getBookmarks(); // Look at initial values of our bookmarks printAllBookmarkInfo(bookmarks); // Obtain bookmarks from a bookmark collection by index/name and update their values bookmarks.get(0).setName("Updated name of " + bookmarks.get(0).getName()); bookmarks.get("MyBookmark 2").setText("Updated text content of " + bookmarks.get(1).getName()); // Remove the latest bookmark // The bookmarked text is not deleted bookmarks.get(2).remove(); bookmarks = doc.getRange().getBookmarks(); // Check that we have 2 bookmarks after the latest bookmark was deleted Assert.assertEquals(bookmarks.getCount(), 2); // Look at updated values of our bookmarks printAllBookmarkInfo(bookmarks); } /// <summary> /// Create a document with bookmarks using the start and end nodes. /// </summary> private static Document createDocumentWithBookmarks() throws Exception { DocumentBuilder builder = new DocumentBuilder(); Document doc = builder.getDocument(); // An empty document has just one empty paragraph by default Paragraph p = doc.getFirstSection().getBody().getFirstParagraph(); // Add several bookmarks to the document for (int i = 1; i <= 3; i++) { String bookmarkName = "MyBookmark " + i; p.appendChild(new Run(doc, "Text before bookmark.")); p.appendChild(new BookmarkStart(doc, bookmarkName)); p.appendChild(new Run(doc, "Text content of " + bookmarkName)); p.appendChild(new BookmarkEnd(doc, bookmarkName)); p.appendChild(new Run(doc, "Text after bookmark.\r\n")); } return builder.getDocument(); } /// <summary> /// Use an iterator and a visitor to print info of every bookmark from within a document. /// </summary> private static void printAllBookmarkInfo(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(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 all bookmarks from a document.// Open a document with 3 bookmarks: "MyBookmark1", "My_Bookmark2", "MyBookmark3" Document doc = new Document(getMyDir() + "Bookmarks.docx"); // Remove all bookmarks from the document // The bookmarked text is not deleted doc.getRange().getBookmarks().clear();
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 3 bookmarks: "MyBookmark 1", "MyBookmark 2", "MyBookmark 3" Document doc = createDocumentWithBookmarks(); BookmarkCollection bookmarks = doc.getRange().getBookmarks(); // Look at initial values of our bookmarks printAllBookmarkInfo(bookmarks); // Obtain bookmarks from a bookmark collection by index/name and update their values bookmarks.get(0).setName("Updated name of " + bookmarks.get(0).getName()); bookmarks.get("MyBookmark 2").setText("Updated text content of " + bookmarks.get(1).getName()); // Remove the latest bookmark // The bookmarked text is not deleted bookmarks.get(2).remove(); bookmarks = doc.getRange().getBookmarks(); // Check that we have 2 bookmarks after the latest bookmark was deleted Assert.assertEquals(bookmarks.getCount(), 2); // Look at updated values of our bookmarks printAllBookmarkInfo(bookmarks); } /// <summary> /// Create a document with bookmarks using the start and end nodes. /// </summary> private static Document createDocumentWithBookmarks() throws Exception { DocumentBuilder builder = new DocumentBuilder(); Document doc = builder.getDocument(); // An empty document has just one empty paragraph by default Paragraph p = doc.getFirstSection().getBody().getFirstParagraph(); // Add several bookmarks to the document for (int i = 1; i <= 3; i++) { String bookmarkName = "MyBookmark " + i; p.appendChild(new Run(doc, "Text before bookmark.")); p.appendChild(new BookmarkStart(doc, bookmarkName)); p.appendChild(new Run(doc, "Text content of " + bookmarkName)); p.appendChild(new BookmarkEnd(doc, bookmarkName)); p.appendChild(new Run(doc, "Text after bookmark.\r\n")); } return builder.getDocument(); } /// <summary> /// Use an iterator and a visitor to print info of every bookmark from within a document. /// </summary> private static void printAllBookmarkInfo(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(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 using different methods.// Open a document with 3 bookmarks: "MyBookmark1", "My_Bookmark2", "MyBookmark3" Document doc = new Document(getMyDir() + "Bookmarks.docx"); // Remove a particular bookmark from the document Bookmark bookmark = doc.getRange().getBookmarks().get(0); doc.getRange().getBookmarks().remove(bookmark); // Remove a bookmark by specified name doc.getRange().getBookmarks().remove("My_Bookmark2"); // Remove a bookmark at the specified index doc.getRange().getBookmarks().removeAt(0);
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 using different methods.// Open a document with 3 bookmarks: "MyBookmark1", "My_Bookmark2", "MyBookmark3" Document doc = new Document(getMyDir() + "Bookmarks.docx"); // Remove a particular bookmark from the document Bookmark bookmark = doc.getRange().getBookmarks().get(0); doc.getRange().getBookmarks().remove(bookmark); // Remove a bookmark by specified name doc.getRange().getBookmarks().remove("My_Bookmark2"); // Remove a bookmark at the specified index doc.getRange().getBookmarks().removeAt(0);
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 using different methods.// Open a document with 3 bookmarks: "MyBookmark1", "My_Bookmark2", "MyBookmark3" Document doc = new Document(getMyDir() + "Bookmarks.docx"); // Remove a particular bookmark from the document Bookmark bookmark = doc.getRange().getBookmarks().get(0); doc.getRange().getBookmarks().remove(bookmark); // Remove a bookmark by specified name doc.getRange().getBookmarks().remove("My_Bookmark2"); // Remove a bookmark at the specified index doc.getRange().getBookmarks().removeAt(0);