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 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();
// Check that we have 3 bookmarks
Assert.assertEquals(bookmarks.getCount(), 3);
// 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 |
BookmarkEnd | getBookmarkEnd() | |
|
Gets the node that represents the end of the bookmark.
|
BookmarkStart | getBookmarkStart() | |
|
Gets the node that represents the start of the bookmark.
|
int | getFirstColumn() | |
|
Gets the zero-based index of the first column of the table column range associated with the bookmark.
|
boolean | isColumn() | |
|
Returns true if this bookmark is a table column bookmark.
|
int | getLastColumn() | |
|
Gets the zero-based index of the last column of the table column range associated with the bookmark.
|
java.lang.String | getName() | |
void | setName(java.lang.String value) | |
|
Gets or sets the name of the bookmark.
|
java.lang.String | getText() | |
void | setText(java.lang.String value) | |
|
Gets or sets the text enclosed in the bookmark.
|
|
Method Summary |
void | remove() | |
|
Removes the bookmark from the document. Does not remove text inside the bookmark.
|
|
Property Getters/Setters Detail |
-
Gets the node that represents the end 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();
// Check that we have 3 bookmarks
Assert.assertEquals(bookmarks.getCount(), 3);
// 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;
}
}
-
Gets the node that represents the start 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();
// Check that we have 3 bookmarks
Assert.assertEquals(bookmarks.getCount(), 3);
// 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;
}
}
getFirstColumn | |
public int getFirstColumn()
|
-
Gets the zero-based index of the first column of the table column range associated with the bookmark.
Returns -1 if this bookmark is not a table column bookmark.
isColumn | |
public boolean isColumn()
|
-
Returns true if this bookmark is a table column bookmark.
getLastColumn | |
public int getLastColumn()
|
-
Gets the zero-based index of the last column of the table column range associated with the bookmark.
Returns -1 if this bookmark is not a table column bookmark.
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.
// Open a document with 3 bookmarks: "MyBookmark1", "My_Bookmark2", "MyBookmark3"
Document doc = new Document(getMyDir() + "Bookmarks.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 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();
// Check that we have 3 bookmarks
Assert.assertEquals(bookmarks.getCount(), 3);
// 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;
}
}
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 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();
// Check that we have 3 bookmarks
Assert.assertEquals(bookmarks.getCount(), 3);
// 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()
throws java.lang.Exception |
-
Removes the bookmark from the document. Does not remove text inside 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();
// Check that we have 3 bookmarks
Assert.assertEquals(bookmarks.getCount(), 3);
// 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;
}
}
See Also:
Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
Aspose.Words Support Forum - our preferred method of support.