This section describes how to use the DocumentBuilder class to easily generate documents or insert rich content and formatting.
DocumentBuilder is a powerful class that is associated with a Document and allows dynamic document building from scratch or the addition of new elements to an existing document. It provides methods to insert text, paragraphs, lists, tables, images and other contents, specification of font, paragraph, and section formatting, and other things. Using DocumentBuilder is somewhat similar in concept to using the StringBuilder class of the .NET Framework.
DocumentBuilder complements classes and methods available in the Aspose.Words Document Object Model by simplifying most common document building tasks, such as inserting text, tables, fields and hyperlinks.
Everything that is possible with DocumentBuilder is also possible when using the classes of the Aspose.Words Document Object Model directly, but using Aspose.Words DOM classes directly usually requires more lines of code than using DocumentBuilder.
DocumentBuilder has an internal cursor that you can navigate to a different location in a document using various MoveToXXX methods. You can insert text, images, bookmarks, form fields, and other document elements at the cursor position using any of InsertXXX and similar methods.
To start, you need to create a DocumentBuilder and associate it with a Document object.
Create a new instance of DocumentBuilder by calling its constructor and pass to it a Document object for attachment to the builder.
Example DocumentBuilderCtor
Shows how to create a simple document using a document builder.
[C#]
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Hello World!");
[Visual Basic]
Dim doc As Document = New Document()
Dim builder As DocumentBuilder = New DocumentBuilder(doc)
builder.Write("Hello World!")
[Java]
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Hello World!");