使用书签

书签在Microsoft Word文档中标识您命名并标识以供将来参考的位置或片段。 例如,您可以使用书签来标识要稍后修改的文本。 您可以使用"书签"对话框转到文档,而不是滚动文档以查找文本。

使用Aspose.Words可以使用书签执行的操作与使用Microsoft Word可以执行的操作相同。 您可以插入新书签,删除,移动到书签,获取或设置书签名称,获取或设置包含在其中的文本。 使用Aspose.Words,您还可以使用报表或文档中的书签将一些数据插入书签或对其内容应用特殊格式。 您还可以使用书签从文档中的某个位置检索文本。

插入书签

使用StartBookmarkEndBookmark通过分别标记书签的开始和结束来创建书签。 不要忘记将相同的书签名称传递给这两种方法。 文档中的书签可以重叠并跨越任何范围。 保存文档时,格式错误的书签或名称重复的书签将被忽略。

下面的代码示例演示如何创建新书签:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directory.
System::String outputDataDir = GetOutputDataDir_WorkingWithBookmarks();
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->StartBookmark(u"My Bookmark");
builder->Writeln(u"Text inside a bookmark.");
builder->StartBookmark(u"Nested Bookmark");
builder->Writeln(u"Text inside a NestedBookmark.");
builder->EndBookmark(u"Nested Bookmark");
builder->Writeln(u"Text after Nested Bookmark.");
builder->EndBookmark(u"My Bookmark");
System::SharedPtr<PdfSaveOptions> options = System::MakeObject<PdfSaveOptions>();
options->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"My Bookmark", 1);
options->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"Nested Bookmark", 2);
System::String outputPath = outputDataDir + u"CreateBookmark.pdf";
doc->Save(outputPath, options);

获取书签

有时需要获取书签集合以遍历书签或用于其他目的。 使用返回表示此节点中包含的文档部分的Range对象的任何文档节点公开的Node.Range属性。 使用此对象检索BookmarkCollection,然后使用集合索引器获取特定书签。

下面的代码示例演示如何从书签集合中获取书签:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directory.
System::String inputDataDir = GetInputDataDir_WorkingWithBookmarks();
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Bookmarks.doc");
// By index.
System::SharedPtr<Bookmark> bookmark1 = doc->get_Range()->get_Bookmarks()->idx_get(0);
// By name.
System::SharedPtr<Bookmark> bookmark2 = doc->get_Range()->get_Bookmarks()->idx_get(u"Bookmark2");

下面的代码示例演示如何获取或设置书签名称和文本:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directory.
System::String inputDataDir = GetInputDataDir_WorkingWithBookmarks();
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Bookmark.doc");
// Use the indexer of the Bookmarks collection to obtain the desired bookmark.
System::SharedPtr<Bookmark> bookmark = doc->get_Range()->get_Bookmarks()->idx_get(u"MyBookmark");
// Get the name and text of the bookmark.
System::String name = bookmark->get_Name();
System::String text = bookmark->get_Text();
// Set the name and text of the bookmark.
bookmark->set_Name(u"RenamedBookmark");
bookmark->set_Text(u"This is a new bookmarked text.");

下面的代码示例演示如何为表添加书签:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Create empty document
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
System::SharedPtr<Table> table = builder->StartTable();
// Insert a cell
builder->InsertCell();
// Start bookmark here after calling InsertCell
builder->StartBookmark(u"MyBookmark");
builder->Write(u"This is row 1 cell 1");
// Insert a cell
builder->InsertCell();
builder->Write(u"This is row 1 cell 2");
builder->EndRow();
// Insert a cell
builder->InsertCell();
builder->Writeln(u"This is row 2 cell 1");
// Insert a cell
builder->InsertCell();
builder->Writeln(u"This is row 2 cell 2");
builder->EndRow();
builder->EndTable();
// End of bookmark
builder->EndBookmark(u"MyBookmark");
System::String outputPath = outputDataDir + u"BookmarkTable.doc";
doc->Save(outputPath);

如果将书签的名称更改为文档中已存在的名称,则不会生成错误,并且在保存文档时只会存储第一个书签。

移动到书签

如果您需要将丰富内容(不仅仅是纯文本)插入书签,则应使用MoveToBookmark将光标移动到书签,然后使用DocumentBuilder’s方法和属性插入内容。

显示隐藏书签内容

整个书签(including the bookmarked content)可以使用Aspose.Words封装在IF字段的真实部分中。 IF字段在表达式(Left of Operator)中包含嵌套的合并字段,并且根据合并字段的值,IF字段显示或隐藏Word文档中的书签内容。

下面的代码示例演示如何显示/隐藏书签:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
typedef System::SharedPtr<System::Object> TObjectPtr;
System::String bookmarkName = u"Bookmark2";
// The path to the documents directory.
System::String inputDataDir = GetInputDataDir_WorkingWithBookmarks();
System::String outputDataDir = GetOutputDataDir_WorkingWithBookmarks();
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Bookmarks.doc");
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
System::SharedPtr<Bookmark> bm = doc->get_Range()->get_Bookmarks()->idx_get(bookmarkName);
builder->MoveToDocumentEnd();
// {IF "{MERGEFIELD bookmark}" = "true" "" ""}
System::SharedPtr<Field> field = builder->InsertField(u"IF \"", nullptr);
builder->MoveTo(field->get_FieldStart()->get_NextSibling());
builder->InsertField(System::String(u"MERGEFIELD ") + bookmarkName + u"", nullptr);
builder->Write(u"\" = \"true\" ");
builder->Write(u"\"");
builder->Write(u"\"");
builder->Write(u" \"\"");
System::SharedPtr<Node> currentNode = field->get_FieldStart();
bool flag = true;
while (currentNode != nullptr && flag)
{
if (currentNode->get_NodeType() == Aspose::Words::NodeType::Run)
{
if (System::ObjectExt::Equals(currentNode->ToString(Aspose::Words::SaveFormat::Text).Trim(), u"\""))
{
flag = false;
}
}
System::SharedPtr<Node> nextNode = currentNode->get_NextSibling();
bm->get_BookmarkStart()->get_ParentNode()->InsertBefore(currentNode, bm->get_BookmarkStart());
currentNode = nextNode;
}
System::SharedPtr<Node> endNode = bm->get_BookmarkEnd();
flag = true;
while (currentNode != nullptr && flag)
{
if (currentNode->get_NodeType() == Aspose::Words::NodeType::FieldEnd)
{
flag = false;
}
System::SharedPtr<Node> nextNode = currentNode->get_NextSibling();
bm->get_BookmarkEnd()->get_ParentNode()->InsertAfter(currentNode, endNode);
endNode = currentNode;
currentNode = nextNode;
}
doc->get_MailMerge()->Execute(System::MakeArray<System::String>({bookmarkName}), System::MakeArray<TObjectPtr>({System::ObjectExt::Box<bool>(false)}));
//MailMerge can be avoided by using the following
//builder.MoveToMergeField(bookmarkName);
//builder.Write(showHide ? "true" : "false");
doc->Save(outputDataDir + u"Updated_Document_out.doc");