DocumentBuilder

DocumentBuilder class

Provides methods to insert text, images and other content, specify font, paragraph and section formatting.

To learn more, visit the Document Builder Overview documentation article.

public class DocumentBuilder

Constructors

NameDescription
DocumentBuilder()Initializes a new instance of this class.
DocumentBuilder(Document)Initializes a new instance of this class.

Properties

NameDescription
Bold { get; set; }True if the font is formatted as bold.
CellFormat { get; }Returns an object that represents current table cell formatting properties.
CurrentNode { get; }Gets the node that is currently selected in this DocumentBuilder.
CurrentParagraph { get; }Gets the paragraph that is currently selected in this DocumentBuilder.
CurrentSection { get; }Gets the section that is currently selected in this DocumentBuilder.
CurrentStory { get; }Gets the story that is currently selected in this DocumentBuilder.
CurrentStructuredDocumentTag { get; }Gets the structured document tag that is currently selected in this DocumentBuilder.
Document { get; set; }Gets or sets the Document object that this object is attached to.
Font { get; }Returns an object that represents current font formatting properties.
IsAtEndOfParagraph { get; }Returns true if the cursor is at the end of the current paragraph.
IsAtEndOfStructuredDocumentTag { get; }Returns true if the cursor is at the end of a structured document tag.
IsAtStartOfParagraph { get; }Returns true if the cursor is at the beginning of the current paragraph (no text before the cursor).
Italic { get; set; }True if the font is formatted as italic.
ListFormat { get; }Returns an object that represents current list formatting properties.
PageSetup { get; }Returns an object that represents current page setup and section properties.
ParagraphFormat { get; }Returns an object that represents current paragraph formatting properties.
RowFormat { get; }Returns an object that represents current table row formatting properties.
Underline { get; set; }Gets/sets underline type for the current font.

Methods

NameDescription
DeleteRow(int, int)Deletes a row from a table.
EndBookmark(string)Marks the current position in the document as a bookmark end.
EndColumnBookmark(string)Marks the current position in the document as a column bookmark end. The position must be in a table cell.
EndEditableRange()Marks the current position in the document as an editable range end.
EndEditableRange(EditableRangeStart)Marks the current position in the document as an editable range end.
EndRow()Ends a table row in the document.
EndTable()Ends a table in the document.
InsertBreak(BreakType)Inserts a break of the specified type into the document.
InsertCell()Inserts a table cell into the document.
InsertChart(ChartType, double, double)Inserts an chart object into the document and scales it to the specified size.
InsertChart(ChartTypeRelativeHorizontalPosition, double, RelativeVerticalPosition, double, double, double, WrapType)Inserts an chart object into the document and scales it to the specified size.
InsertCheckBox(string, bool, int)Inserts a checkbox form field at the current position.
InsertCheckBox(string, bool, bool, int)Inserts a checkbox form field at the current position.
InsertComboBox(string, string[], int)Inserts a combobox form field at the current position.
InsertDocument(DocumentImportFormatMode)Inserts a document at the cursor position.
InsertDocument(DocumentImportFormatModeImportFormatOptions)Inserts a document at the cursor position.
InsertDocumentInline(DocumentImportFormatModeImportFormatOptions)Inserts a document inline at the cursor position.
InsertField(string)Inserts a Word field into a document and updates the field result.
InsertField(FieldType, bool)Inserts a Word field into a document and optionally updates the field result.
InsertField(string, string)Inserts a Word field into a document without updating the field result.
InsertFootnote(FootnoteType, string)Inserts a footnote or endnote into the document.
InsertFootnote(FootnoteType, string, string)Inserts a footnote or endnote into the document.
InsertHorizontalRule()Inserts a horizontal rule shape into the document.
InsertHtml(string)Inserts an HTML string into the document.
InsertHtml(string, bool)Inserts an HTML string into the document.
InsertHtml(string, HtmlInsertOptions)Inserts an HTML string into the document. Allows to specify additional options.
InsertHyperlink(string, string, bool)Inserts a hyperlink into the document.
InsertImage(byte[])Inserts an image from a byte array into the document. The image is inserted inline and at 100% scale.
InsertImage(Image)Inserts an image from a .NET Image object into the document. The image is inserted inline and at 100% scale.
InsertImage(Stream)Inserts an image from a stream into the document. The image is inserted inline and at 100% scale.
InsertImage(string)Inserts an image from a file or URL into the document. The image is inserted inline and at 100% scale.
InsertImage(byte[], double, double)Inserts an inline image from a byte array into the document and scales it to the specified size.
InsertImage(Image, double, double)Inserts an inline image from a .NET Image object into the document and scales it to the specified size.
InsertImage(Stream, double, double)Inserts an inline image from a stream into the document and scales it to the specified size.
InsertImage(string, double, double)Inserts an inline image from a file or URL into the document and scales it to the specified size.
InsertImage(byte[], RelativeHorizontalPosition, double, RelativeVerticalPosition, double, double, double, WrapType)Inserts an image from a byte array at the specified position and size.
InsertImage(Image, RelativeHorizontalPosition, double, RelativeVerticalPosition, double, double, double, WrapType)Inserts an image from a .NET Image object at the specified position and size.
InsertImage(Stream, RelativeHorizontalPosition, double, RelativeVerticalPosition, double, double, double, WrapType)Inserts an image from a stream at the specified position and size.
InsertImage(string, RelativeHorizontalPosition, double, RelativeVerticalPosition, double, double, double, WrapType)Inserts an image from a file or URL at the specified position and size.
InsertNode(Node)Inserts a node before the cursor.
InsertOleObject(Stream, string, bool, Stream)Inserts an embedded OLE object from a stream into the document.
InsertOleObject(string, bool, bool, Stream)Inserts an embedded or linked OLE object from a file into the document. Detects OLE object type using file extension.
InsertOleObject(string, string, bool, bool, Stream)Inserts an embedded or linked OLE object from a file into the document. Detects OLE object type using given progID parameter.
InsertOleObjectAsIcon(Stream, string, string, string)Inserts an embedded OLE object as icon from a stream into the document. Allows to specify icon file and caption. Detects OLE object type using given progID parameter.
InsertOleObjectAsIcon(string, bool, string, string)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.
InsertOleObjectAsIcon(string, string, bool, string, string)Inserts an embedded or linked OLE object as icon into the document. Allows to specify icon file and caption. Detects OLE object type using given progID parameter.
InsertOnlineVideo(string, double, double)Inserts an online video object into the document and scales it to the specified size.
InsertOnlineVideo(string, string, byte[], double, double)Inserts an online video object into the document and scales it to the specified size.
InsertOnlineVideo(string, RelativeHorizontalPosition, double, RelativeVerticalPosition, double, double, double, WrapType)Inserts an online video object into the document and scales it to the specified size.
InsertOnlineVideo(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.
InsertParagraph()Inserts a paragraph break into the document.
InsertShape(ShapeType, double, double)Inserts inline shape with specified type and size.
InsertShape(ShapeTypeRelativeHorizontalPosition, double, RelativeVerticalPosition, double, double, double, WrapType)Inserts free-floating shape with specified position, size and text wrap type.
InsertSignatureLine(SignatureLineOptions)Inserts a signature line at the current position.
InsertSignatureLine(SignatureLineOptionsRelativeHorizontalPosition, double, RelativeVerticalPosition, double, WrapType)Inserts a signature line at the specified position.
InsertStyleSeparator()Inserts style separator into the document.
InsertTableOfContents(string)Inserts a TOC (table of contents) field into the document.
InsertTextInput(string, TextFormFieldType, string, string, int)Inserts a text form field at the current position.
MoveTo(Node)Moves the cursor to an inline node or to the end of a paragraph.
MoveToBookmark(string)Moves the cursor to a bookmark.
MoveToBookmark(string, bool, bool)Moves the cursor to a bookmark with greater precision.
MoveToCell(int, int, int, int)Moves the cursor to a table cell in the current section.
MoveToDocumentEnd()Moves the cursor to the end of the document.
MoveToDocumentStart()Moves the cursor to the beginning of the document.
MoveToField(Field, bool)Moves the cursor to a field in the document.
MoveToHeaderFooter(HeaderFooterType)Moves the cursor to the beginning of a header or footer in the current section.
MoveToMergeField(string)Moves the cursor to a position just beyond the specified merge field and removes the merge field.
MoveToMergeField(string, bool, bool)Moves the merge field to the specified merge field.
MoveToParagraph(int, int)Moves the cursor to a paragraph in the current section.
MoveToSection(int)Moves the cursor to the beginning of the body in a specified section.
MoveToStructuredDocumentTag(int, int)Moves the cursor to a structured document tag in the current section.
MoveToStructuredDocumentTag(StructuredDocumentTag, int)Moves the cursor to the structured document tag.
PopFont()Retrieves character formatting previously saved on the stack.
PushFont()Saves current character formatting onto the stack.
StartBookmark(string)Marks the current position in the document as a bookmark start.
StartColumnBookmark(string)Marks the current position in the document as a column bookmark start. The position must be in a table cell.
StartEditableRange()Marks the current position in the document as an editable range start.
StartTable()Starts a table in the document.
Write(string)Inserts a string into the document at the current insert position.
Writeln()Inserts a paragraph break into the document.
Writeln(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, Writeln, InsertBreak 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 use a document builder to create a table.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Start the table, then populate the first row with two cells.
builder.StartTable();
builder.InsertCell();
builder.Write("Row 1, Cell 1.");
builder.InsertCell();
builder.Write("Row 1, Cell 2.");

// Call the builder's "EndRow" method to start a new row.
builder.EndRow();
builder.InsertCell();
builder.Write("Row 2, Cell 1.");
builder.InsertCell();
builder.Write("Row 2, Cell 2.");
builder.EndTable();

doc.Save(ArtifactsDir + "DocumentBuilder.CreateTable.docx");

Shows how to create headers and footers in a document using DocumentBuilder.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

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

// Create the headers, then add three pages to the document to display each header type.
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
builder.Write("Header for the first page");
builder.MoveToHeaderFooter(HeaderFooterType.HeaderEven);
builder.Write("Header for even pages");
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("Header for all other pages");

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.docx");

Shows how to build a table with custom borders.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.StartTable();

// Setting table formatting options for a document builder
// will apply them to every row and cell that we add with it.
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();

// Changing the formatting will apply it to the current cell,
// and any new cells that we create with the builder afterward.
// This will not affect the cells that we have added previously.
builder.CellFormat.Shading.ClearFormatting();

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

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

builder.EndRow();

// Increase row height to fit the vertical text.
builder.InsertCell();
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();

doc.Save(ArtifactsDir + "DocumentBuilder.InsertTable.docx");

See Also