使用范围
Contents
[
Hide
]
在Aspose.Words中,范围是文档的树状模型的"平面窗口"。
如果您使用过Microsoft WordAutomation,您可能知道检查和修改文档内容的主要工具之一是Range
对象。范围就像文档内容和格式的"窗口"。
Aspose.Words也有Range类,它的外观和行为类似于Microsoft Word中的Range。 虽然Range不能复盖文档的任意部分并且没有Start和End,但您可以访问包括Document本身在内的任何文档节点所复盖的范围。 换句话说,每个节点都有自己的范围。 Range对象允许您访问和修改范围内的文本、书签和表单字段。
检索纯文本
使用Text属性检索范围的纯文本、无格式文本。
下面的代码示例演示如何获取范围的纯文本、无格式文本:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
您可以从以下位置下载此示例的示例文件 Aspose.Words GitHub.
删除文本
Range
类允许通过调用delete删除范围的所有字符。
下面的代码示例演示如何删除范围的所有字符:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
您可以从以下位置下载此示例的示例文件 Aspose.Words GitHub.