範囲の操作

Microsoft Word自動化を使用したことがある場合は、ドキュメントの内容を調べて変更するための主要なツールの1つが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");