DocumentBuilder class

DocumentBuilder class

Adapts DocumentBuilder class public API for Node.js porting.

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 DocumentBuilder.write(), DocumentBuilder.writeln(), DocumentBuilder.insertBreak() and other methods. You can navigate the DocumentBuilder cursor to a different location in a document using various MoveToXXX methods.

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

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

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

Use the DocumentBuilder.cellFormat and DocumentBuilder.rowFormat properties to specify formatting properties for table cells and rows. User the DocumentBuilder.insertCell() and DocumentBuilder.endRow() methods to build a table.

Note that DocumentBuilder.font, DocumentBuilder.paragraphFormat and DocumentBuilder.pageSetup properties are updated whenever you navigate to a different place in the document to reflect formatting properties available at the new location.

Constructors

NameDescription
DocumentBuilder()Initializes a new instance of this class.
DocumentBuilder(options)Initializes a new instance of this class.
DocumentBuilder(doc)Initializes a new instance of this class.
DocumentBuilder(doc, options)Initializes a new instance of this class.

Properties

NameDescription
boldTrue if the font is formatted as bold.
cellFormatReturns an object that represents current table cell formatting properties.
currentNodeGets the node that is currently selected in this DocumentBuilder.
currentParagraphGets the paragraph that is currently selected in this DocumentBuilder.
currentSectionGets the section that is currently selected in this DocumentBuilder.
currentStoryGets the story that is currently selected in this DocumentBuilder.
currentStructuredDocumentTagGets the structured document tag that is currently selected in this DocumentBuilder.
documentGets or sets the DocumentBuilder.document object that this object is attached to.
fontReturns an object that represents current font formatting properties.
isAtEndOfParagraphReturns true if the cursor is at the end of the current paragraph.
isAtEndOfStructuredDocumentTagReturns true if the cursor is at the end of a structured document tag.
isAtStartOfParagraphReturns true if the cursor is at the beginning of the current paragraph (no text before the cursor).
italicTrue if the font is formatted as italic.
listFormatReturns an object that represents current list formatting properties.
pageSetupReturns an object that represents current page setup and section properties.
paragraphFormatReturns an object that represents current paragraph formatting properties.
rowFormatReturns an object that represents current table row formatting properties.
underlineGets/sets underline type for the current font.

Methods

NameDescription
deleteRow(tableIndex, rowIndex)Deletes a row from a table.
endBookmark(bookmarkName)Marks the current position in the document as a bookmark end.
endColumnBookmark(bookmarkName)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(start)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, width, height)Inserts an chart object into the document and scales it to the specified size.
insertChart(chartType, width, height, chartStyle)Inserts an chart object into the document and scales it to the specified size.
insertChart(chartType, horzPos, left, vertPos, top, width, height, wrapType)Inserts an chart object into the document and scales it to the specified size.
insertChart(chartType, horzPos, left, vertPos, top, width, height, wrapType, chartStyle)Inserts an chart object into the document and scales it to the specified size.
insertCheckBox(name, checkedValue, size)Inserts a checkbox form field at the current position.
insertCheckBox(name, defaultValue, checkedValue, size)Inserts a checkbox form field at the current position.
insertComboBox(name, items, selectedIndex)Inserts a combobox form field at the current position.
insertDocument(srcDoc, importFormatMode)Inserts a document at the cursor position.
insertDocument(srcDoc, importFormatMode, importFormatOptions)Inserts a document at the cursor position.
insertDocumentInline(srcDoc, importFormatMode, importFormatOptions)Inserts a document inline at the cursor position.
insertField(fieldType, updateField)Inserts a Word field into a document and optionally updates the field result.
insertField(fieldCode)Inserts a Word field into a document and updates the field result.
insertField(fieldCode, fieldValue)Inserts a Word field into a document without updating the field result.
insertFootnote(footnoteType, footnoteText)Inserts a footnote or endnote into the document.
insertFootnote(footnoteType, footnoteText, referenceMark)Inserts a footnote or endnote into the document.
insertForms2OleControl(forms2OleControl)Inserts Forms2OleControl object into current position.
insertGroupShape(shapes)Groups the shapes passed as a parameter into a new GroupShape node which is inserted into the current position.
insertGroupShape(left, top, width, height, shapes)Groups the shapes passed as a parameter into a new GroupShape node of the specified size which is inserted into the specified position.
insertHorizontalRule()Inserts a horizontal rule shape into the document.
insertHtml(html)Inserts an HTML string into the document.
insertHtml(html, useBuilderFormatting)Inserts an HTML string into the document.
insertHtml(html, options)Inserts an HTML string into the document. Allows to specify additional options.
insertHyperlink(displayText, urlOrBookmark, isBookmark)Inserts a hyperlink into the document.
insertImage(image)
insertImage(image, width, height)
insertImage(image, horzPos, left, vertPos, top, width, height, wrapType)
insertImage(fileName)Inserts an image from a file or URL 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(imageBytes)Inserts an image from a byte array into the document. The image is inserted inline and at 100% scale.
insertImage(fileName, width, height)Inserts an inline image from a file or URL into the document and scales it to the specified size.
insertImage(stream, width, height)Inserts an inline image from a stream into the document and scales it to the specified size.
insertImage(imageBytes, width, height)Inserts an inline image from a byte array into the document and scales it to the specified size.
insertImage(fileName, horzPos, left, vertPos, top, width, height, wrapType)Inserts an image from a file or URL at the specified position and size.
insertImage(stream, horzPos, left, vertPos, top, width, height, wrapType)Inserts an image from a stream at the specified position and size.
insertImage(imageBytes, horzPos, left, vertPos, top, width, height, wrapType)Inserts an image from a byte array at the specified position and size.
insertNode(node)Inserts a node before the cursor.
insertOleObject(stream, progId, asIcon, presentation)Inserts an embedded OLE object from a stream into the document.
insertOleObject(fileName, isLinked, asIcon, presentation)Inserts an embedded or linked OLE object from a file into the document. Detects OLE object type using file extension.
insertOleObject(fileName, progId, isLinked, asIcon, presentation)Inserts an embedded or linked OLE object from a file into the document. Detects OLE object type using given progID parameter.
insertOleObjectAsIcon(fileName, isLinked, iconFile, iconCaption)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(fileName, progId, isLinked, iconFile, iconCaption)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.
insertOleObjectAsIcon(stream, progId, iconFile, iconCaption)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.
insertOnlineVideo(videoUrl, width, height)Inserts an online video object into the document and scales it to the specified size.
insertOnlineVideo(videoUrl, horzPos, left, vertPos, top, width, height, wrapType)Inserts an online video object into the document and scales it to the specified size.
insertOnlineVideo(videoUrl, videoEmbedCode, thumbnailImageBytes, width, height)Inserts an online video object into the document and scales it to the specified size.
insertOnlineVideo(videoUrl, videoEmbedCode, thumbnailImageBytes, horzPos, left, vertPos, top, width, height, 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, width, height)Inserts inline shape with specified type and size.
insertShape(shapeType, horzPos, left, vertPos, top, width, height, wrapType)Inserts free-floating shape with specified position, size and text wrap type.
insertSignatureLine(signatureLineOptions)Inserts a signature line at the current position.
insertSignatureLine(signatureLineOptions, horzPos, left, vertPos, top, wrapType)Inserts a signature line at the specified position.
insertStructuredDocumentTag(type)Inserts a StructuredDocumentTag into the document.
insertStyleSeparator()Inserts style separator into the document.
insertTableOfContents(switches)Inserts a TOC (table of contents) field into the document.
insertTextInput(name, type, format, fieldValue, maxLength)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(bookmarkName)Moves the cursor to a bookmark.
moveToBookmark(bookmarkName, isStart, isAfter)Moves the cursor to a bookmark with greater precision.
moveToCell(tableIndex, rowIndex, columnIndex, characterIndex)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, isAfter)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(fieldName)Moves the cursor to a position just beyond the specified merge field and removes the merge field.
moveToMergeField(fieldName, isAfter, isDeleteField)Moves the merge field to the specified merge field.
moveToParagraph(paragraphIndex, characterIndex)Moves the cursor to a paragraph in the current section.
moveToSection(sectionIndex)Moves the cursor to the beginning of the body in a specified section.
moveToStructuredDocumentTag(structuredDocumentTagIndex, characterIndex)Moves the cursor to a structured document tag in the current section.
moveToStructuredDocumentTag(structuredDocumentTag, characterIndex)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(bookmarkName)Marks the current position in the document as a bookmark start.
startColumnBookmark(bookmarkName)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(text)Inserts a string into the document at the current insert position.
writeln(text)Inserts a string and a paragraph break into the document.
writeln()Inserts a paragraph break into the document.

Examples

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

let doc = new aw.Document();
let builder = new aw.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(aw.HeaderFooterType.HeaderFirst);
builder.write("Header for the first page");
builder.moveToHeaderFooter(aw.HeaderFooterType.HeaderEven);
builder.write("Header for even pages");
builder.moveToHeaderFooter(aw.HeaderFooterType.HeaderPrimary);
builder.write("Header for all other pages");

builder.moveToSection(0);
builder.writeln("Page1");
builder.insertBreak(aw.BreakType.PageBreak);
builder.writeln("Page2");
builder.insertBreak(aw.BreakType.PageBreak);
builder.writeln("Page3");

doc.save(base.artifactsDir + "DocumentBuilder.HeadersAndFooters.docx");

Shows how to build a table with custom borders.

let doc = new aw.Document();
let builder = new aw.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 = aw.ParagraphAlignment.Center;

builder.cellFormat.clearFormatting();
builder.cellFormat.width = 150;
builder.cellFormat.verticalAlignment = aw.Tables.CellVerticalAlignment.Center;
builder.cellFormat.shading.backgroundPatternColor = "#ADFF2F";
builder.cellFormat.wrapText = false;
builder.cellFormat.fitText = true;

builder.rowFormat.clearFormatting();
builder.rowFormat.heightRule = aw.HeightRule.Exactly;
builder.rowFormat.height = 50;
builder.rowFormat.borders.lineStyle = aw.LineStyle.Engrave3D;
builder.rowFormat.borders.color = "#FFA500";

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 = aw.TextOrientation.Upward;
builder.write("Row 3, Col 1");

builder.insertCell();
builder.cellFormat.orientation = aw.TextOrientation.Downward;
builder.write("Row 3, Col 2");

builder.endRow();
builder.endTable();

doc.save(base.artifactsDir + "DocumentBuilder.InsertTable.docx");

Shows how to use a document builder to create a table.

let doc = new aw.Document();
let builder = new aw.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(base.artifactsDir + "DocumentBuilder.CreateTable.docx");

See Also