範囲の操作

Microsoft Word オートメーションを使用したことがある場合は、ドキュメントのコンテンツを調べて変更するための主要なツールの 1 つが Range オブジェクトであることをご存知でしょう。 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-.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();