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

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

DocumentBuilder

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

DocumentBuilder

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

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

ドキュメントのナビゲーションは、MoveToDocumentStartMoveToFieldなどのさまざまなDocumentBuilder.MoveToXXXメソッドを使用してドキュメント内の別の場所に移動できる仮想カーソルの概念に基づ この仮想カーソルは、メソッドを呼び出すときにテキストが挿入される場所を示しますWrite, Writeln, InsertBreak, と他の人。

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

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"DocumentBuilder.doc");
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->MoveToBookmark(u"CoolBookmark", false, true);
builder->Writeln(u"This is a very cool bookmark.");

文書の作成と変更

Aspose.WordsAPIには、文書のさまざまな要素の書式設定を担当するいくつかのクラスが用意されています。 各クラスは、text、paragraph、sectionなどの特定のドキュメント要素に関連する書式設定プロパティをカプセル化します。 たとえば、Fontクラスは文字の書式設定プロパティを表し、ParagraphFormatクラスは段落の書式設定プロパティを表します。 これらのクラスのオブジェクトは、クラスと同じ名前を持つ対応するDocumentBuilderプロパティによって返されます。 したがって、ドキュメントのビルド中にそれらにアクセスして目的の書式設定を設定できます。

テキスト、チェックボックス、oleオブジェクト、画像、ブックマーク、フォームフィールド、およびその他のドキュメント要素をWriteメソッドまたはInsertFieldInsertHtmlなどのDocumentBuilder.InsertXXX

DocumentBuilderを使用して簡単な文書を作成する方法を見てみましょう。

DocumentBuilderを使用して文書を作成する

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

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

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

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Create a blank document.
System::SharedPtr<Document> doc = System::MakeObject<Document>();
// DocumentBuilder provides members to easily add content to a document.
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
// Write a new paragraph in the document with the text "Hello World!"
builder->Writeln(u"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-C
// The path to the documents directory.
System::String outputDataDir = GetOutputDataDir_WorkingWithDocument();
// Initialize document.
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
// Specify font formatting
System::SharedPtr<Font> font = builder->get_Font();
font->set_Size(16);
font->set_Bold(true);
font->set_Color(System::Drawing::Color::get_Blue());
font->set_Name(u"Arial");
font->set_Underline(Underline::Dash);
// Specify paragraph formatting
System::SharedPtr<ParagraphFormat> paragraphFormat = builder->get_ParagraphFormat();
paragraphFormat->set_FirstLineIndent(8);
paragraphFormat->set_Alignment(ParagraphAlignment::Justify);
paragraphFormat->set_KeepTogether(true);
builder->Writeln(u"A whole paragraph.");
System::String outputPath = outputDataDir + u"DocumentBuilderInsertParagraph.doc";
doc->Save(outputPath);