ドキュメントビルダーの概要

DocumentBuilderDocument に関連付けられた強力なクラスで、動的ドキュメントを最初から作成したり、既存のドキュメントに新しい要素を追加したりできるようにします。

DocumentBuilder は、テキスト、チェックボックス、OLE オブジェクト、段落、リスト、表、画像、およびその他のコンテンツ要素を挿入するメソッドを提供します。これにより、フォント、段落またはセクションの書式設定を指定したり、その他の操作を実行したりできます。

ドキュメントビルダーまたはAspose.Words DOM

DocumentBuilder は、Aspose.Words Document Object Model (DOM) で使用可能なクラスとメソッドを補完し、最も一般的なドキュメント構築タスクを簡素化します。つまり、ツリー構造をよく理解する必要がある Aspose.Words DOM と DocumentBuilder の両方を使用して、ドキュメントのコンテンツを作成および変更できます。 DocumentBuilder は複雑な Document 構造の「ファサード」であり、コンテンツと書式設定を迅速かつ簡単に挿入できるようになります。

DocumentBuilder で可能な操作は、Aspose.Words DOM のクラスを直接使用する場合にも可能です。ただし、Aspose.Words DOM クラスを直接使用すると、通常、DocumentBuilder を使用する場合よりも多くのコード行が必要になります。

ドキュメントナビゲーション

ドキュメント ナビゲーションは仮想カーソルの概念に基づいており、MoveToDocumentStartMoveToField などのさまざまな DocumentBuilder.MoveToXXX メソッドを使用してドキュメント内の別の場所に移動できます。この仮想カーソルは、WriteWritelnInsertBreak などのメソッドを呼び出すときにテキストが挿入される場所を示します。仮想カーソルの詳細については、次の記事「カーソルを使用したナビゲーション」を参照してください。

次のコード例は、ブックマークに移動する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "DocumentBuilder.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark("CoolBookmark", false, true);
builder.Writeln("This is a very cool bookmark.");

ドキュメントの構築と変更

Aspose.Words API は、ドキュメントのさまざまな要素のフォーマットを担当するいくつかのクラスを提供します。各クラスは、テキスト、段落、セクションなどの特定の文書要素に関連する書式設定プロパティをカプセル化します。たとえば、Font クラスは文字書式設定プロパティを表し、ParagraphFormat クラスは段落書式設定プロパティを表します。これらのクラスのオブジェクトは、クラスと同じ名前を持つ、対応する DocumentBuilder プロパティによって返されます。したがって、ドキュメントの作成中にこれらにアクセスして必要な書式を設定できます。

また、Write メソッドや、InsertFieldInsertHtml、類似のメソッドなどの DocumentBuilder.InsertXXX メソッドを使用して、テキスト、チェックボックス、OLE オブジェクト、画像、ブックマーク、フォーム フィールド、その他のドキュメント要素をカーソル位置に挿入することもできます。

DocumentBuilder を使用して簡単なドキュメントを作成する方法を見てみましょう。

DocumentBuilder を使用してドキュメントを作成する

まず、DocumentBuilder を作成し、それを Document オブジェクトに関連付ける必要があります。コンストラクターを呼び出して DocumentBuilder の新しいインスタンスを作成し、それをビルダーに接続するための Document オブジェクトに渡します。

テキストを挿入するには、ドキュメントに挿入する必要があるテキストの文字列を Write メソッドに渡します。

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Hello, world!");

ドキュメントの書式設定を指定する

Font プロパティはテキストの書式設定を定義します。このオブジェクトには、さまざまなフォント属性 (フォント名、フォント サイズ、色など) が含まれています。いくつかの重要なフォント属性は DocumentBuilder プロパティでも表され、直接アクセスできるようになります。これらは、Font.BoldFont.Italic、および Font.Underline のブール型プロパティです。

次のコード例は、DocumentBuilder を使用して書式設定されたテキストを挿入する方法を示しています。

// 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_WorkingWithDocument();
// Initialize document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify font formatting
Font font = builder.Font;
font.Size = 16;
font.Bold = true;
font.Color = System.Drawing.Color.Blue;
font.Name = "Arial";
font.Underline = Underline.Dash;
// Specify paragraph formatting
ParagraphFormat paragraphFormat = builder.ParagraphFormat;
paragraphFormat.FirstLineIndent = 8;
paragraphFormat.Alignment = ParagraphAlignment.Justify;
paragraphFormat.KeepTogether = true;
builder.Writeln("A whole paragraph.");
dataDir = dataDir + "DocumentBuilderInsertParagraph_out.doc";
doc.Save(dataDir);