使用书签
书签在 Microsoft Word 文档中标识您命名和标识的位置或片段,以供将来参考。例如,您可以使用书签来标识稍后要修改的文本。您可以使用"书签"对话框转到该文本,而不是滚动文档来查找文本。
使用 Aspose.Words 对书签执行的操作与使用 Microsoft Word 执行的操作相同。您可以插入新书签、删除、移动到书签、获取或设置书签名称、获取或设置其中包含的文本。
插入书签
使用 StartBookmark 和 EndBookmark 分别通过标记其开始和结束来创建书签。不要忘记将相同的书签名称传递给这两个方法。文档中的书签可以重叠并跨越任何范围。保存文档时,格式错误的书签或名称重复的书签将被忽略。
以下代码示例展示了如何创建新书签:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithBookmarks(); | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.StartBookmark("My Bookmark"); | |
builder.Writeln("Text inside a bookmark."); | |
builder.StartBookmark("Nested Bookmark"); | |
builder.Writeln("Text inside a NestedBookmark."); | |
builder.EndBookmark("Nested Bookmark"); | |
builder.Writeln("Text after Nested Bookmark."); | |
builder.EndBookmark("My Bookmark"); | |
PdfSaveOptions options = new PdfSaveOptions(); | |
options.OutlineOptions.BookmarksOutlineLevels.Add("My Bookmark", 1); | |
options.OutlineOptions.BookmarksOutlineLevels.Add("Nested Bookmark", 2); | |
dataDir = dataDir + "Create.Bookmark_out.pdf"; | |
doc.Save(dataDir, options); |
获取书签
有时需要获取书签集合以迭代书签或用于其他目的。使用任何文档节点公开的 Node.Range 属性,该属性返回表示该节点中包含的文档部分的 Range 对象。使用此对象检索 BookmarkCollection,然后使用集合索引器获取特定书签。
以下代码示例演示如何从书签集合中获取书签:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithBookmarks(); | |
Document doc = new Document(dataDir + "Bookmarks.doc"); | |
// By index. | |
Bookmark bookmark1 = doc.Range.Bookmarks[0]; | |
// By name. | |
Bookmark bookmark2 = doc.Range.Bookmarks["Bookmark2"]; |
以下代码示例显示如何获取或设置书签名称和文本:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithBookmarks(); | |
Document doc = new Document(dataDir + "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."; |
以下代码示例显示如何为表添加书签:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// Create empty document | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Table table = builder.StartTable(); | |
// Insert a cell | |
builder.InsertCell(); | |
// Start bookmark here after calling InsertCell | |
builder.StartBookmark("MyBookmark"); | |
builder.Write("This is row 1 cell 1"); | |
// Insert a cell | |
builder.InsertCell(); | |
builder.Write("This is row 1 cell 2"); | |
builder.EndRow(); | |
// Insert a cell | |
builder.InsertCell(); | |
builder.Writeln("This is row 2 cell 1"); | |
// Insert a cell | |
builder.InsertCell(); | |
builder.Writeln("This is row 2 cell 2"); | |
builder.EndRow(); | |
builder.EndTable(); | |
// End of bookmark | |
builder.EndBookmark("MyBookmark"); | |
dataDir = dataDir + "Bookmark.Table_out.doc"; | |
doc.Save(dataDir); |
如果将书签名称更改为文档中已存在的名称,则保存文档时不会产生错误,并且只会存储第一个书签。
请注意,文档中的某些书签已分配给表单字段。移动到这样的书签并在其中插入文本会将文本插入表单字段代码中。虽然这不会使表单字段无效,但插入的文本将不可见,因为它成为字段代码的一部分。
以下代码示例显示如何访问已添加书签的表的列:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// Create empty document | |
Document doc = new Document(dataDir + "Bookmark.Table_out.doc"); | |
foreach (Bookmark bookmark in doc.Range.Bookmarks) | |
{ | |
Console.WriteLine("Bookmark: {0}{1}", bookmark.Name, bookmark.IsColumn ? " (Column)" : ""); | |
if (bookmark.IsColumn) | |
{ | |
Row row = bookmark.BookmarkStart.GetAncestor(NodeType.Row) as Row; | |
if (row != null && bookmark.FirstColumn < row.Cells.Count) | |
Console.WriteLine(row.Cells[bookmark.FirstColumn].GetText().TrimEnd(ControlChar.CellChar)); | |
} | |
} |
移至书签
如果需要在书签中插入丰富的内容(不仅仅是纯文本),则应使用 MoveToBookmark 将光标移动到书签,然后使用 DocumentBuilder’s 方法和属性插入内容。
显示隐藏书签内容
整个书签(包括加书签的内容)可以使用 Aspose.Words 封装在 IF
字段的 True 部分中。可以这样的方式,IF
字段在表达式(操作符的左边)中包含嵌套的合并字段,并且根据合并字段的值,IF
字段显示或隐藏Word文档中书签的内容。
以下代码示例展示了如何显示/隐藏书签:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithBookmarks(); | |
Document doc = new Document(dataDir + "Bookmarks.doc"); | |
ShowHideBookmarkedContent(doc, "Bookmark2", false); | |
doc.Save(dataDir + "Updated_Document.doc"); | |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
public static void ShowHideBookmarkedContent(Document doc, String bookmarkName, bool showHide) | |
{ | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Bookmark bm = doc.Range.Bookmarks[bookmarkName]; | |
builder.MoveToDocumentEnd(); | |
// {IF "{MERGEFIELD bookmark}" = "true" "" ""} | |
Field field = builder.InsertField("IF \"", null); | |
builder.MoveTo(field.Start.NextSibling); | |
builder.InsertField("MERGEFIELD " + bookmarkName + "", null); | |
builder.Write("\" = \"true\" "); | |
builder.Write("\""); | |
builder.Write("\""); | |
builder.Write(" \"\""); | |
Node currentNode = field.Start; | |
bool flag = true; | |
while (currentNode != null && flag) | |
{ | |
if (currentNode.NodeType == NodeType.Run) | |
if (currentNode.ToString(SaveFormat.Text).Trim().Equals("\"")) | |
flag = false; | |
Node nextNode = currentNode.NextSibling; | |
bm.BookmarkStart.ParentNode.InsertBefore(currentNode, bm.BookmarkStart); | |
currentNode = nextNode; | |
} | |
Node endNode = bm.BookmarkEnd; | |
flag = true; | |
while (currentNode != null && flag) | |
{ | |
if (currentNode.NodeType == NodeType.FieldEnd) | |
flag = false; | |
Node nextNode = currentNode.NextSibling; | |
bm.BookmarkEnd.ParentNode.InsertAfter(currentNode, endNode); | |
endNode = currentNode; | |
currentNode = nextNode; | |
} | |
doc.MailMerge.Execute(new String[] { bookmarkName }, new Object[] { showHide }); | |
//MailMerge can be avoided by using the following | |
//builder.MoveToMergeField(bookmarkName); | |
//builder.Write(showHide ? "true" : "false"); | |
} |