在Microsoft Word里咱们常使用书签,Aspose.Word书签功能也可以完全实现word的效果,就是让你标识和命名位置的工具,以便以后能够快速查阅,这样的话就无需在文档中上下滚动来定位该文。只需要双击书签,光标就自动字位到关键字这一行,然后选中,是不是很方便呢。
Aspose.Word可以插入一个新的书签、删除书签、移动书签,设置书签的名字、文本等。下面咱们简单介绍Aspose.Word中书签的几个简单用法:
1.使用Aspose.Word快速创建一个新的书签,具体代码如下:
[C#]
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.StartBookmark("MyBookmark");
builder.Writeln("Text inside a bookmark.");
builder.EndBookmark("MyBookmark");
[Visual Basic]
Dim doc As New Document()
Dim builder As New DocumentBuilder(doc)
builder.StartBookmark("MyBookmark")
builder.Writeln("Text inside a bookmark.")
builder.EndBookmark("MyBookmark")
2.如何获取word里里的书签,在word里经常会存在多个书签,控件提供了方法用于查找和获取word里的书签,具体代码如下:
[C#]
Document doc = new Document(MyDir + "Bookmarks.doc");
Bookmark bookmark1 = doc.Range.Bookmarks[0];
Bookmark bookmark2 = doc.Range.Bookmarks["Bookmark2"];
[Visual Basic]
Dim doc As New Document(MyDir & "Bookmarks.doc")
Dim bookmark1 As Bookmark = doc.Range.Bookmarks(0)
Dim bookmark2 As Bookmark = doc.Range.Bookmarks("Bookmark2")
3. 如何得到和设置书签名字和文本,具体代码如下:
[C#]
Document doc = new Document(MyDir + "Bookmark.doc");
// Use the indexer of the Bookmarks collection to obtain the desired bookmark.
Bookmark bookmark = doc.Range.Bookmarks["MyBookmark"];
// Get the name and text of the bookmark.
string name = bookmark.Name;
string text = bookmark.Text;
// Set the name and text of the bookmark.
bookmark.Name = "RenamedBookmark";
bookmark.Text = "This is a new bookmarked text.";
[VB.NET]
Dim doc As New Document(MyDir & "Bookmark.doc")
' Use the indexer of the Bookmarks collection to obtain the desired bookmark.
Dim bookmark As Bookmark = doc.Range.Bookmarks("MyBookmark")
' Get the name and text of the bookmark.
Dim name As String = bookmark.Name
Dim text As String = bookmark.Text
' Set the name and text of the bookmark.
bookmark.Name = "RenamedBookmark"
bookmark.Text = "This is a new bookmarked text."
联系客服