文档彩票走势图>>Aspose中文文档>>获取和设置书签文本
获取和设置书签文本
Aspose.Words是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
使用Aspose.Words
在Aspose.Words中,使用Bookmark类来处理单个书签,并使用BookmarkCollection类来处理书签对象的集合。
以下代码示例演示如何获取和设置文档中的书签文本:
// For complete examples and data files, please go to //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.";
点击复制
使用 Open XML SDK
需要添加的命名空间:
using System.Collections.Generic; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using NUnit.Framework;
点击复制
以下代码示例演示如何获取和设置文档中的书签文本:
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"; } } } }
点击复制