java.lang.Objectcom.aspose.words.Bookmark
public class Bookmark
Example:
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 | ||
---|---|---|
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 |
---|
getBookmarkEnd | |
public BookmarkEnd getBookmarkEnd() |
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() |
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; } }
getFirstColumn | |
public int getFirstColumn() |
isColumn | |
public boolean isColumn() |
getLastColumn | |
public int getLastColumn() |
getName/setName | |
public java.lang.String getName() / public void setName(java.lang.String value) |
Example:
Shows how to replace elements in bookmark nameDocument 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) |
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 |
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();