使用范围
如果您使用过 Microsoft Word Automation,您可能知道检查和修改文档内容的主要工具之一是 Range 对象。 Range 就像一个了解文档内容和格式的"窗口"。
Aspose.Words 还具有 Range 类,其设计的外观和行为与 Microsoft Word 中的 Range 类似。尽管 Range 无法覆盖文档的任意部分,并且没有 Start 和 End,但您可以访问任何文档节点(包括 Document 本身)覆盖的范围。换句话说,每个节点都有自己的范围。 Range 对象允许您访问和修改范围内的文本、书签和表单字段。
检索纯文本
使用 Text 属性检索范围内的纯文本、无格式文本。
以下代码示例演示如何获取范围内的纯文本、无格式文本:
// 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_WorkingWithRanges(); | |
Document doc = new Document(dataDir + "Document.doc"); | |
string text = doc.Range.Text; |
删除文本
Range允许通过调用Delete来删除该范围内的所有字符。
以下代码示例显示如何删除某个范围内的所有字符:
// 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_WorkingWithRanges(); | |
Document doc = new Document(dataDir + "Document.doc"); | |
doc.Sections[0].Range.Delete(); |