文檔首頁>>Aspose中文文檔>>獲取和設(shè)置書簽文本
獲取和設(shè)置書簽文本
Aspose.Words是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無需在跨平臺應(yīng)用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式處理,并允許將各類文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
使用Aspose.Words
在Aspose.Words中,使用Bookmark類來處理單個(gè)書簽,并使用BookmarkCollection類來處理書簽對象的集合。
以下代碼示例演示如何獲取和設(shè)置文檔中的書簽文本:
// 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.";
點(diǎn)擊復(fù)制
使用 Open XML SDK
需要添加的命名空間:
using System.Collections.Generic; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using NUnit.Framework;
點(diǎn)擊復(fù)制
以下代碼示例演示如何獲取和設(shè)置文檔中的書簽文本:
public void GetAndSetBookmarkTextFeature() { IDictionary<string, BookmarkStart> bookmarkMap = new Dictionary<string, BookmarkStart>(); using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(MyDir + "Get and set bookmark text.docx", true)) { foreach (BookmarkStart bookmarkStart in wordDocument.MainDocumentPart.Document.Body.Descendants<BookmarkStart>()) { bookmarkMap[bookmarkStart.Name] = bookmarkStart; foreach (BookmarkStart bookmark in bookmarkMap.Values) { Run bookmarkText = bookmark.NextSibling<Run>() if (bookmarkText != null) bookmarkText.GetFirstChild<Text>().Text = "Test"; } } } }
點(diǎn)擊復(fù)制