ドキュメントの作成またはロード

Aspose.Words を使用して実行するほぼすべてのタスクには、ドキュメントのロードが含まれます。 Document クラスは、メモリにロードされたドキュメントを表します。ドキュメントにはいくつかのオーバーロードされたコンストラクターがあり、空のドキュメントを作成したり、ファイルまたはストリームからロードしたりできます。ドキュメントは、Aspose.Words がサポートする任意のロード形式でロードできます。サポートされているすべてのロード形式のリストについては、LoadFormat 列挙を参照してください。

新しいドキュメントの作成

パラメーターを指定せずに Document コンストラクターを呼び出して、新しい空のドキュメントを作成します。プログラムでドキュメントを生成する場合、最も簡単な方法は、DocumentBuilder クラスを使用してドキュメントのコンテンツを追加することです。

次のコード例は、ドキュメント ビルダーを使用してドキュメントを作成する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Hello World!");
doc.Save(ArtifactsDir + "AddContentUsingDocumentBuilder.CreateNewDocument.docx");
view raw create-docx.cs hosted with ❤ by GitHub

ドキュメント{#load-a-document}をロードする

既存のドキュメントを LoadFormat 形式でロードするには、ファイル名またはストリームを Document コンストラクターの 1 つに渡します。ロードされたドキュメントの形式は、拡張子によって自動的に決定されます。

ファイルからロード

ファイル名を文字列として Document コンストラクターに渡し、ファイルから既存のドキュメントを開きます。

次のコード例は、ファイルからドキュメントを開く方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(MyDir + "Document.docx");
view raw load-docx.cs hosted with ❤ by GitHub

この例のテンプレート ファイルは Aspose.Words GitHub からダウンロードできます。

ストリーム {#load-from-a-stream} からのロード

ストリームからドキュメントを開くには、ドキュメントを含むストリーム オブジェクトを Document コンストラクターに渡すだけです。

次のコード例は、ストリームからドキュメントを開く方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Read only access is enough for Aspose.Words to load a document.
Stream stream = File.OpenRead(MyDir + "Document.docx");
Document doc = new Document(stream);
// You can close the stream now, it is no longer needed because the document is in memory.
stream.Close();