DocumentBuilder Class

Provides methods to insert text, images and other content, specify font, paragraph and section formatting.
Inheritance Hierarchy
SystemObject
  Aspose.WordsDocumentBuilder

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public class DocumentBuilder

The DocumentBuilder type exposes the following members.

Constructors
  NameDescription
Public methodCode exampleDocumentBuilder
Initializes a new instance of this class.
Public methodCode exampleDocumentBuilder(Document)
Initializes a new instance of this class.
Properties
  NameDescription
Public propertyCode exampleBold
True if the font is formatted as bold.
Public propertyCode exampleCellFormat
Returns an object that represents current table cell formatting properties.
Public propertyCode exampleCurrentNode
Gets the node that is currently selected in this DocumentBuilder.
Public propertyCode exampleCurrentParagraph
Gets the paragraph that is currently selected in this DocumentBuilder.
Public propertyCode exampleCurrentSection
Gets the section that is currently selected in this DocumentBuilder.
Public propertyCode exampleCurrentStory
Gets the story that is currently selected in this DocumentBuilder.
Public propertyCode exampleDocument
Gets or sets the Document object that this object is attached to.
Public propertyCode exampleFont
Returns an object that represents current font formatting properties.
Public propertyCode exampleIsAtEndOfParagraph
Returns true if the cursor is at the end of the current paragraph.
Public propertyCode exampleIsAtStartOfParagraph
Returns true if the cursor is at the beginning of the current paragraph (no text before the cursor).
Public propertyCode exampleItalic
True if the font is formatted as italic.
Public propertyCode exampleListFormat
Returns an object that represents current list formatting properties.
Public propertyCode examplePageSetup
Returns an object that represents current page setup and section properties.
Public propertyCode exampleParagraphFormat
Returns an object that represents current paragraph formatting properties.
Public propertyCode exampleRowFormat
Returns an object that represents current table row formatting properties.
Public propertyCode exampleUnderline
Gets/sets underline type for the current font.
Methods
  NameDescription
Public methodCode exampleDeleteRow
Deletes a row from a table.
Public methodCode exampleEndBookmark
Marks the current position in the document as a bookmark end.
Public methodCode exampleEndEditableRange
Marks the current position in the document as an editable range end.
Public methodCode exampleEndEditableRange(EditableRangeStart)
Marks the current position in the document as an editable range end.
Public methodCode exampleEndRow
Ends a table row in the document.
Public methodCode exampleEndTable
Ends a table in the document.
Public methodEquals (Inherited from Object.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Public methodCode exampleInsertBreak
Inserts a break of the specified type into the document.
Public methodCode exampleInsertCell
Inserts a table cell into the document.
Public methodCode exampleInsertChart(ChartType, Double, Double)
Inserts an chart object into the document and scales it to the specified size.
Public methodCode exampleInsertChart(ChartType, RelativeHorizontalPosition, Double, RelativeVerticalPosition, Double, Double, Double, WrapType)
Inserts an chart object into the document and scales it to the specified size.
Public methodCode exampleInsertCheckBox(String, Boolean, Int32)
Inserts a checkbox form field at the current position.
Public methodCode exampleInsertCheckBox(String, Boolean, Boolean, Int32)
Inserts a checkbox form field at the current position.
Public methodCode exampleInsertComboBox
Inserts a combobox form field at the current position.
Public methodCode exampleInsertDocument(Document, ImportFormatMode)
Inserts a document at the cursor position.
Public methodCode exampleInsertDocument(Document, ImportFormatMode, ImportFormatOptions)
Inserts a document at the cursor position.
Public methodCode exampleInsertField(String)
Inserts a Word field into a document and updates the field result.
Public methodCode exampleInsertField(String, String)
Inserts a Word field into a document without updating the field result.
Public methodCode exampleInsertField(FieldType, Boolean)
Inserts a Word field into a document and optionally updates the field result.
Public methodCode exampleInsertFootnote(FootnoteType, String)
Inserts a footnote or endnote into the document.
Public methodCode exampleInsertFootnote(FootnoteType, String, String)
Inserts a footnote or endnote into the document.
Public methodCode exampleInsertHorizontalRule
Inserts a horizontal rule shape into the document.
Public methodCode exampleInsertHtml(String)
Inserts an HTML string into the document.
Public methodCode exampleInsertHtml(String, Boolean)
Inserts an HTML string into the document.
Public methodCode exampleInsertHyperlink
Inserts a hyperlink into the document.
Public methodCode exampleInsertImage(Byte)
Inserts an image from a byte array into the document. The image is inserted inline and at 100% scale.
Public methodCode exampleInsertImage(Image)
Inserts an image from a .NET Image object into the document. The image is inserted inline and at 100% scale.
Public methodCode exampleInsertImage(Stream)
Inserts an image from a stream into the document. The image is inserted inline and at 100% scale.
Public methodCode exampleInsertImage(String)
Inserts an image from a file or URL into the document. The image is inserted inline and at 100% scale.
Public methodCode exampleInsertImage(Byte, Double, Double)
Inserts an inline image from a byte array into the document and scales it to the specified size.
Public methodCode exampleInsertImage(Image, Double, Double)
Inserts an inline image from a .NET Image object into the document and scales it to the specified size.
Public methodCode exampleInsertImage(Stream, Double, Double)
Inserts an inline image from a stream into the document and scales it to the specified size.
Public methodCode exampleInsertImage(String, Double, Double)
Inserts an inline image from a file or URL into the document and scales it to the specified size.
Public methodCode exampleInsertImage(Byte, RelativeHorizontalPosition, Double, RelativeVerticalPosition, Double, Double, Double, WrapType)
Inserts an image from a byte array at the specified position and size.
Public methodCode exampleInsertImage(Image, RelativeHorizontalPosition, Double, RelativeVerticalPosition, Double, Double, Double, WrapType)
Inserts an image from a .NET Image object at the specified position and size.
Public methodCode exampleInsertImage(Stream, RelativeHorizontalPosition, Double, RelativeVerticalPosition, Double, Double, Double, WrapType)
Inserts an image from a stream at the specified position and size.
Public methodCode exampleInsertImage(String, RelativeHorizontalPosition, Double, RelativeVerticalPosition, Double, Double, Double, WrapType)
Inserts an image from a file or URL at the specified position and size.
Public methodCode exampleInsertNode
Inserts a text level node inside the current paragraph before the cursor.
Public methodCode exampleInsertOleObject(Stream, String, Boolean, Image)
Inserts an embedded OLE object from a stream into the document.
Public methodCode exampleInsertOleObject(String, Boolean, Boolean, Image)
Inserts an embedded or linked OLE object from a file into the document. Detects OLE object type using file extension.
Public methodCode exampleInsertOleObject(String, String, Boolean, Boolean, Image)
Inserts an embedded or linked OLE object from a file into the document. Detects OLE object type using given progID parameter.
Public methodCode exampleInsertOleObjectAsIcon
Inserts an embedded or linked OLE object as icon into the document. Allows to specify icon file and caption. Detects OLE object type using file extension.
Public methodCode exampleInsertOnlineVideo(String, Double, Double)
Inserts an online video object into the document and scales it to the specified size.
Public methodCode exampleInsertOnlineVideo(String, String, Byte, Double, Double)
Inserts an online video object into the document and scales it to the specified size.
Public methodCode exampleInsertOnlineVideo(String, RelativeHorizontalPosition, Double, RelativeVerticalPosition, Double, Double, Double, WrapType)
Inserts an online video object into the document and scales it to the specified size.
Public methodCode exampleInsertOnlineVideo(String, String, Byte, RelativeHorizontalPosition, Double, RelativeVerticalPosition, Double, Double, Double, WrapType)
Inserts an online video object into the document and scales it to the specified size.
Public methodCode exampleInsertParagraph
Inserts a paragraph break into the document.
Public methodCode exampleInsertShape(ShapeType, Double, Double)
Inserts inline shape with specified type and size.
Public methodCode exampleInsertShape(ShapeType, RelativeHorizontalPosition, Double, RelativeVerticalPosition, Double, Double, Double, WrapType)
Inserts free-floating shape with specified position, size and text wrap type.
Public methodCode exampleInsertSignatureLine(SignatureLineOptions)
Inserts a signature line at the current position.
Public methodCode exampleInsertSignatureLine(SignatureLineOptions, RelativeHorizontalPosition, Double, RelativeVerticalPosition, Double, WrapType)
Inserts a signature line at the specified position.
Public methodCode exampleInsertStyleSeparator
Inserts style separator into the document.
Public methodCode exampleInsertTableOfContents
Inserts a TOC (table of contents) field into the document.
Public methodCode exampleInsertTextInput
Inserts a text form field at the current position.
Public methodCode exampleMoveTo
Moves the cursor to an inline node or to the end of a paragraph.
Public methodCode exampleMoveToBookmark(String)
Moves the cursor to a bookmark.
Public methodCode exampleMoveToBookmark(String, Boolean, Boolean)
Moves the cursor to a bookmark with greater precision.
Public methodCode exampleMoveToCell
Moves the cursor to a table cell in the current section.
Public methodCode exampleMoveToDocumentEnd
Moves the cursor to the end of the document.
Public methodCode exampleMoveToDocumentStart
Moves the cursor to the beginning of the document.
Public methodCode exampleMoveToField
Moves the cursor to a field in the document.
Public methodCode exampleMoveToHeaderFooter
Moves the cursor to the beginning of a header or footer in the current section.
Public methodCode exampleMoveToMergeField(String)
Moves the cursor to a position just beyond the specified merge field and removes the merge field.
Public methodCode exampleMoveToMergeField(String, Boolean, Boolean)
Moves the merge field to the specified merge field.
Public methodCode exampleMoveToParagraph
Moves the cursor to a paragraph in the current section.
Public methodCode exampleMoveToSection
Moves the cursor to the beginning of the body in a specified section.
Public methodCode examplePopFont
Retrieves character formatting previously saved on the stack.
Public methodCode examplePushFont
Saves current character formatting onto the stack.
Public methodCode exampleStartBookmark
Marks the current position in the document as a bookmark start.
Public methodCode exampleStartEditableRange
Marks the current position in the document as an editable range start.
Public methodCode exampleStartTable
Starts a table in the document.
Public methodToString (Inherited from Object.)
Public methodCode exampleWrite
Inserts a string into the document at the current insert position.
Public methodCode exampleWriteln
Inserts a paragraph break into the document.
Public methodCode exampleWriteln(String)
Inserts a string and a paragraph break into the document.
Remarks

DocumentBuilder makes the process of building a Document easier. Document is a composite object consisting of a tree of nodes and while inserting content nodes directly into the tree is possible, it requires good understanding of the tree structure. DocumentBuilder is a "facade" for the complex structure of Document and allows to insert content and formatting quickly and easily.

Create a DocumentBuilder and associate it with a Document.

The DocumentBuilder has an internal cursor where the text will be inserted when you call Write(String), Writeln(String), InsertBreak(BreakType) and other methods. You can navigate the DocumentBuilder cursor to a different location in a document using various MoveToXXX methods.

Use the Font property to specify character formatting that will apply to all text inserted from the current position in the document onwards.

Use the ParagraphFormat property to specify paragraph formatting for the current and all paragraphs that will be inserted.

Use the PageSetup property to specify page and section properties for the current section and all section that will be inserted.

Use the CellFormat and RowFormat properties to specify formatting properties for table cells and rows. User the InsertCell and EndRow methods to build a table.

Note that Font, ParagraphFormat and PageSetup properties are updated whenever you navigate to a different place in the document to reflect formatting properties available at the new location.

Examples
Shows how to create a simple table using DocumentBuilder with default formatting.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// We call this method to start building the table
builder.StartTable();
builder.InsertCell();
builder.Write("Row 1, Cell 1 Content.");

// Build the second cell
builder.InsertCell();
builder.Write("Row 1, Cell 2 Content.");
// Call the following method to end the row and start a new row
builder.EndRow();

// Build the first cell of the second row
builder.InsertCell();
builder.Write("Row 2, Cell 1 Content");

// Build the second cell.
builder.InsertCell();
builder.Write("Row 2, Cell 2 Content.");
builder.EndRow();

// Signal that we have finished building the table
builder.EndTable();

// Save the document to disk
doc.Save(ArtifactsDir + "DocumentBuilder.CreateSimpleTable.doc");
Examples
Creates headers and footers in a document using DocumentBuilder.
// Create a blank document
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Specify that we want headers and footers different for first, even and odd pages
builder.PageSetup.DifferentFirstPageHeaderFooter = true;
builder.PageSetup.OddAndEvenPagesHeaderFooter = true;

// Create the headers
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
builder.Write("Header First");
builder.MoveToHeaderFooter(HeaderFooterType.HeaderEven);
builder.Write("Header Even");
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("Header Odd");

// Create three pages in the document
builder.MoveToSection(0);
builder.Writeln("Page1");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Page2");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Page3");

doc.Save(ArtifactsDir + "DocumentBuilder.HeadersAndFooters.doc");
Examples
Shows how to build a nice bordered table.
DocumentBuilder builder = new DocumentBuilder();

// Start building a table
builder.StartTable();

// Set the appropriate paragraph, cell, and row formatting. The formatting properties are preserved
// until they are explicitly modified so there's no need to set them for each row or cell
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

builder.CellFormat.ClearFormatting();
builder.CellFormat.Width = 150;
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.CellFormat.Shading.BackgroundPatternColor = Color.GreenYellow;
builder.CellFormat.WrapText = false;
builder.CellFormat.FitText = true;

builder.RowFormat.ClearFormatting();
builder.RowFormat.HeightRule = HeightRule.Exactly;
builder.RowFormat.Height = 50;
builder.RowFormat.Borders.LineStyle = LineStyle.Engrave3D;
builder.RowFormat.Borders.Color = Color.Orange;

builder.InsertCell();
builder.Write("Row 1, Col 1");

builder.InsertCell();
builder.Write("Row 1, Col 2");

builder.EndRow();

// Remove the shading (clear background)
builder.CellFormat.Shading.ClearFormatting();

builder.InsertCell();
builder.Write("Row 2, Col 1");

builder.InsertCell();
builder.Write("Row 2, Col 2");

builder.EndRow();

builder.InsertCell();

// Make the row height bigger so that a vertically oriented text could fit into cells
builder.RowFormat.Height = 150;
builder.CellFormat.Orientation = TextOrientation.Upward;
builder.Write("Row 3, Col 1");

builder.InsertCell();
builder.CellFormat.Orientation = TextOrientation.Downward;
builder.Write("Row 3, Col 2");

builder.EndRow();

builder.EndTable();

builder.Document.Save(ArtifactsDir + "DocumentBuilder.InsertTable.docx");
See Also