與書籤一起工作
書籤在 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 方法與屬性來插入內容。
顯示藏起書籤內容
書籤(包括被標記的內容)可以封裝在 IF
欄中的 True 部分,並使用 Aspose.Words。 它可以設定在表達式中 (運算子左邊), 其中 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"); | |
} |