ドキュメントビルダーの概要
DocumentBuilderはDocumentに関連付けられた強力なクラスで、動的なドキュメントを最初から作成したり、既存のドキュメントに新しい要素を追加したりできます。DocumentBuilderは、Documentに関連付けられた強力なクラスです。
DocumentBuilder
ドキュメントビルダーまたはAspose.WordsDOM
DocumentBuilder
DocumentBuilderで可能な操作は、Aspose.WordsDOMのクラスを直接使用する場合にも可能です。 ただし、Aspose.WordsDOMクラスを直接使用するには、通常、DocumentBuilderを使用するよりも多くのコード行が必要です。
ドキュメントナビゲーション
文書ナビゲーションは仮想カーソルの概念に基づいており、MoveToDocumentStartやMoveToFieldなどのさまざまなDocumentBuilder.MoveToXXXメソッドを使用して文書内の別の場所に移動できます。 この仮想カーソルは、メソッドを呼び出すときにテキストが挿入される場所を示しますWrite, Writeln, InsertBreak, と他の人。 仮想カーソルの詳細については、次の記事"カーソルを使用したナビゲーション"を参照してください。
次のコード例は、ブックマークに移動する方法を示しています:
// 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(DocumentBuilderMoveToBookmarkEnd.class); | |
// Open the document. | |
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."); | |
doc.save(dataDir + "output.doc"); |
文書の作成と変更
Aspose.WordsAPIは、文書のさまざまな要素の書式設定を担当するいくつかのクラスを提供します。 各クラスは、text、paragraph、sectionなどの特定のドキュメント要素に関連する書式設定プロパティをカプセル化します。 たとえば、Fontクラスは文字の書式設定プロパティを表し、ParagraphFormatクラスは段落の書式設定プロパティを表します。 これらのクラスのオブジェクトは、クラスと同じ名前を持つ対応するDocumentBuilderプロパティによって返されます。 したがって、ドキュメントのビルド中にそれらにアクセスして目的の書式設定を設定できます。
テキスト、checkbox、oleオブジェクト、画像、ブックマーク、フォームフィールド、その他のドキュメント要素をWrite
メソッドまたはInsertField、InsertHtmlなどの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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(HelloWorld.class); | |
// Create a blank document. | |
Document doc = new Document(); | |
// DocumentBuilder provides members to easily add content to a document. | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Write a new paragraph in the document with the text "Hello World!" | |
builder.writeln("Hello World!"); | |
// Save the document in DOCX format. The format to save as is inferred from the extension of the file name. | |
// Aspose.Words supports saving any document in many more formats. | |
doc.save(dataDir + "HelloWorld_out_.docx"); |
文書の書式設定を指定する
Fontプロパティはテキストの書式設定を定義します。 このオブジェクトには、さまざまなフォント属性(フォント名、フォントサイズ、色など)が含まれています。 いくつかの重要なフォント属性は、直接アクセスできるようにDocumentBuilderプロパティでも表されます。 これらは、Font.Bold、Font.Italic、およびFont.Underlineブールプロパティです。
次のコード例は、DocumentBuilderを使用して書式設定されたテキストを挿入する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Font font = builder.getFont(); | |
font.setSize(16); | |
font.setColor(Color.DARK_GRAY); | |
font.setBold(true); | |
font.setName("Algerian"); | |
font.setUnderline(2); | |
ParagraphFormat paragraphFormat = builder.getParagraphFormat(); | |
paragraphFormat.setFirstLineIndent(12); | |
paragraphFormat.setAlignment(1); | |
paragraphFormat.setKeepTogether(true); | |
builder.write("This is a sample Paragraph"); | |
doc.save(dataDir + "InsertParagraph_out.doc"); |
- Fontは、文書内の現在の位置から挿入されたすべてのテキストに適用される文字書式を指定します。
- ParagraphFormat挿入する現在の段落とすべての段落の段落書式を指定します。
- PageSetupは、現在のセクションおよび挿入されるセクション全体のページおよびセクションプロパティを指定します。
- CellFormatとRowFormatは、文書内の現在の位置からテーブルのセルと行に適用される書式設定プロパティを指定します。
この場合、「現在」とは、カーソルが置かれている位置、段落、セクション、セル、または行を意味します。