使用范围

如果您使用过Microsoft WordAutomation,您可能知道检查和修改文档内容的主要工具之一是Range对象。范围就像文档内容和格式的"窗口"。

Aspose.Words也有Range类,它的外观和行为类似于Microsoft Word中的Range。 虽然Range不能复盖文档的任意部分并且没有StartEnd,但您可以访问包括Document本身在内的任何文档节点所复盖的范围。 换句话说,每个节点都有自己的范围。 Range对象允许您访问和修改范围内的文本、书签和表单字段。

检索纯文本

使用Text属性检索范围的纯文本、无格式文本。

下面的代码示例演示如何获取范围的纯文本、无格式文本:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(RangesGetText.class);
Document doc = new Document(dataDir + "Document.doc");
String text = doc.getText();
System.out.println(text);

删除文本

Range类允许通过调用delete删除范围的所有字符。

下面的代码示例演示如何删除范围的所有字符:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(RangesDeleteText.class);
Document doc = new Document(dataDir + "Document.doc");
doc.getSections().get(0).getRange().delete();
doc.save(dataDir + "output.doc");