DocumentBuilder class

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.

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.insert_break() 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.paragraph_format property to specify paragraph formatting for the current and all paragraphs that will be inserted.

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

Use the DocumentBuilder.cell_format and DocumentBuilder.row_format properties to specify formatting properties for table cells and rows. User the DocumentBuilder.insert_cell() and DocumentBuilder.end_row() methods to build a table.

Note that DocumentBuilder.font, DocumentBuilder.paragraph_format and DocumentBuilder.page_setup 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(doc)Initializes a new instance of this class.

Properties

NameDescription
boldTrue if the font is formatted as bold.
cell_formatReturns an object that represents current table cell formatting properties.
current_nodeGets the node that is currently selected in this DocumentBuilder.
current_paragraphGets the paragraph that is currently selected in this DocumentBuilder.
current_sectionGets the section that is currently selected in this DocumentBuilder.
current_storyGets the story that is currently selected in this DocumentBuilder.
current_structured_document_tagGets 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.
is_at_end_of_paragraphReturns True if the cursor is at the end of the current paragraph.
is_at_end_of_structured_document_tagReturns true if the cursor is at the end of a structured document tag.
is_at_start_of_paragraphReturns 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.
list_formatReturns an object that represents current list formatting properties.
page_setupReturns an object that represents current page setup and section properties.
paragraph_formatReturns an object that represents current paragraph formatting properties.
row_formatReturns an object that represents current table row formatting properties.
underlineGets/sets underline type for the current font.

Methods

NameDescription
delete_row(table_index, row_index)Deletes a row from a table.
end_bookmark(bookmark_name)Marks the current position in the document as a bookmark end.
end_column_bookmark(bookmark_name)Marks the current position in the document as a column bookmark end. The position must be in a table cell.
end_editable_range()Marks the current position in the document as an editable range end.
end_editable_range(start)Marks the current position in the document as an editable range end.
end_row()Ends a table row in the document.
end_table()Ends a table in the document.
insert_break(break_type)Inserts a break of the specified type into the document.
insert_cell()Inserts a table cell into the document.
insert_chart(chart_type, width, height)Inserts an chart object into the document and scales it to the specified size.
insert_chart(chart_type, horz_pos, left, vert_pos, top, width, height, wrap_type)Inserts an chart object into the document and scales it to the specified size.
insert_check_box(name, checked_value, size)Inserts a checkbox form field at the current position.
insert_check_box(name, default_value, checked_value, size)Inserts a checkbox form field at the current position.
insert_combo_box(name, items, selected_index)Inserts a combobox form field at the current position.
insert_document(src_doc, import_format_mode)Inserts a document at the cursor position.
insert_document(src_doc, import_format_mode, import_format_options)Inserts a document at the cursor position.
insert_document_inline(src_doc, import_format_mode, import_format_options)Inserts a document inline at the cursor position.
insert_field(field_type, update_field)Inserts a Word field into a document and optionally updates the field result.
insert_field(field_code)Inserts a Word field into a document and updates the field result.
insert_field(field_code, field_value)Inserts a Word field into a document without updating the field result.
insert_footnote(footnote_type, footnote_text)Inserts a footnote or endnote into the document.
insert_footnote(footnote_type, footnote_text, reference_mark)Inserts a footnote or endnote into the document.
insert_horizontal_rule()Inserts a horizontal rule shape into the document.
insert_html(html)Inserts an HTML string into the document.
insert_html(html, use_builder_formatting)Inserts an HTML string into the document.
insert_html(html, options)Inserts an HTML string into the document. Allows to specify additional options.
insert_hyperlink(display_text, url_or_bookmark, is_bookmark)Inserts a hyperlink into the document.
insert_image(file_name)Inserts an image from a file or URL into the document. The image is inserted inline and at 100% scale.
insert_image(stream)Inserts an image from a stream into the document. The image is inserted inline and at 100% scale.
insert_image(image_bytes)Inserts an image from a byte array into the document. The image is inserted inline and at 100% scale.
insert_image(file_name, width, height)Inserts an inline image from a file or URL into the document and scales it to the specified size.
insert_image(stream, width, height)Inserts an inline image from a stream into the document and scales it to the specified size.
insert_image(image_bytes, width, height)Inserts an inline image from a byte array into the document and scales it to the specified size.
insert_image(file_name, horz_pos, left, vert_pos, top, width, height, wrap_type)Inserts an image from a file or URL at the specified position and size.
insert_image(stream, horz_pos, left, vert_pos, top, width, height, wrap_type)Inserts an image from a stream at the specified position and size.
insert_image(image_bytes, horz_pos, left, vert_pos, top, width, height, wrap_type)Inserts an image from a byte array at the specified position and size.
insert_node(node)Inserts a node before the cursor.
insert_ole_object(stream, prog_id, as_icon, presentation)Inserts an embedded OLE object from a stream into the document.
insert_ole_object(file_name, is_linked, as_icon, presentation)Inserts an embedded or linked OLE object from a file into the document. Detects OLE object type using file extension.
insert_ole_object(file_name, prog_id, is_linked, as_icon, presentation)Inserts an embedded or linked OLE object from a file into the document. Detects OLE object type using given progID parameter.
insert_ole_object_as_icon(file_name, is_linked, icon_file, icon_caption)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.
insert_ole_object_as_icon(file_name, prog_id, is_linked, icon_file, icon_caption)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.
insert_ole_object_as_icon(stream, prog_id, icon_file, icon_caption)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.
insert_online_video(video_url, width, height)Inserts an online video object into the document and scales it to the specified size.
insert_online_video(video_url, horz_pos, left, vert_pos, top, width, height, wrap_type)Inserts an online video object into the document and scales it to the specified size.
insert_online_video(video_url, video_embed_code, thumbnail_image_bytes, width, height)Inserts an online video object into the document and scales it to the specified size.
insert_online_video(video_url, video_embed_code, thumbnail_image_bytes, horz_pos, left, vert_pos, top, width, height, wrap_type)Inserts an online video object into the document and scales it to the specified size.
insert_paragraph()Inserts a paragraph break into the document.
insert_shape(shape_type, width, height)Inserts inline shape with specified type and size.
insert_shape(shape_type, horz_pos, left, vert_pos, top, width, height, wrap_type)Inserts free-floating shape with specified position, size and text wrap type.
insert_signature_line(signature_line_options)Inserts a signature line at the current position.
insert_signature_line(signature_line_options, horz_pos, left, vert_pos, top, wrap_type)Inserts a signature line at the specified position.
insert_style_separator()Inserts style separator into the document.
insert_table_of_contents(switches)Inserts a TOC (table of contents) field into the document.
insert_text_input(name, type, format, field_value, max_length)Inserts a text form field at the current position.
move_to(node)Moves the cursor to an inline node or to the end of a paragraph.
move_to_bookmark(bookmark_name)Moves the cursor to a bookmark.
move_to_bookmark(bookmark_name, is_start, is_after)Moves the cursor to a bookmark with greater precision.
move_to_cell(table_index, row_index, column_index, character_index)Moves the cursor to a table cell in the current section.
move_to_document_end()Moves the cursor to the end of the document.
move_to_document_start()Moves the cursor to the beginning of the document.
move_to_field(field, is_after)Moves the cursor to a field in the document.
move_to_header_footer(header_footer_type)Moves the cursor to the beginning of a header or footer in the current section.
move_to_merge_field(field_name)Moves the cursor to a position just beyond the specified merge field and removes the merge field.
move_to_merge_field(field_name, is_after, is_delete_field)Moves the merge field to the specified merge field.
move_to_paragraph(paragraph_index, character_index)Moves the cursor to a paragraph in the current section.
move_to_section(section_index)Moves the cursor to the beginning of the body in a specified section.
move_to_structured_document_tag(structured_document_tag_index, character_index)Moves the cursor to a structured document tag in the current section.
move_to_structured_document_tag(structured_document_tag, character_index)Moves the cursor to the structured document tag.
pop_font()Retrieves character formatting previously saved on the stack.
push_font()Saves current character formatting onto the stack.
start_bookmark(bookmark_name)Marks the current position in the document as a bookmark start.
start_column_bookmark(bookmark_name)Marks the current position in the document as a column bookmark start. The position must be in a table cell.
start_editable_range()Marks the current position in the document as an editable range start.
start_table()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.

doc = aw.Document()
builder = aw.DocumentBuilder(doc)
# Specify that we want different headers and footers for first, even and odd pages.
builder.page_setup.different_first_page_header_footer = True
builder.page_setup.odd_and_even_pages_header_footer = True
# Create the headers, then add three pages to the document to display each header type.
builder.move_to_header_footer(aw.HeaderFooterType.HEADER_FIRST)
builder.write('Header for the first page')
builder.move_to_header_footer(aw.HeaderFooterType.HEADER_EVEN)
builder.write('Header for even pages')
builder.move_to_header_footer(aw.HeaderFooterType.HEADER_PRIMARY)
builder.write('Header for all other pages')
builder.move_to_section(0)
builder.writeln('Page1')
builder.insert_break(aw.BreakType.PAGE_BREAK)
builder.writeln('Page2')
builder.insert_break(aw.BreakType.PAGE_BREAK)
builder.writeln('Page3')
doc.save(file_name=ARTIFACTS_DIR + 'DocumentBuilder.HeadersAndFooters.docx')

Shows how to build a table with custom borders.

doc = aw.Document()
builder = aw.DocumentBuilder(doc)
builder.start_table()
# Setting table formatting options for a document builder
# will apply them to every row and cell that we add with it.
builder.paragraph_format.alignment = aw.ParagraphAlignment.CENTER
builder.cell_format.clear_formatting()
builder.cell_format.width = 150
builder.cell_format.vertical_alignment = aw.tables.CellVerticalAlignment.CENTER
builder.cell_format.shading.background_pattern_color = aspose.pydrawing.Color.green_yellow
builder.cell_format.wrap_text = False
builder.cell_format.fit_text = True
builder.row_format.clear_formatting()
builder.row_format.height_rule = aw.HeightRule.EXACTLY
builder.row_format.height = 50
builder.row_format.borders.line_style = aw.LineStyle.ENGRAVE_3D
builder.row_format.borders.color = aspose.pydrawing.Color.orange
builder.insert_cell()
builder.write('Row 1, Col 1')
builder.insert_cell()
builder.write('Row 1, Col 2')
builder.end_row()
# 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.cell_format.shading.clear_formatting()
builder.insert_cell()
builder.write('Row 2, Col 1')
builder.insert_cell()
builder.write('Row 2, Col 2')
builder.end_row()
# Increase row height to fit the vertical text.
builder.insert_cell()
builder.row_format.height = 150
builder.cell_format.orientation = aw.TextOrientation.UPWARD
builder.write('Row 3, Col 1')
builder.insert_cell()
builder.cell_format.orientation = aw.TextOrientation.DOWNWARD
builder.write('Row 3, Col 2')
builder.end_row()
builder.end_table()
doc.save(file_name=ARTIFACTS_DIR + 'DocumentBuilder.InsertTable.docx')

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

doc = aw.Document()
builder = aw.DocumentBuilder(doc)
# Start the table, then populate the first row with two cells.
builder.start_table()
builder.insert_cell()
builder.write('Row 1, Cell 1.')
builder.insert_cell()
builder.write('Row 1, Cell 2.')
# Call the builder's "EndRow" method to start a new row.
builder.end_row()
builder.insert_cell()
builder.write('Row 2, Cell 1.')
builder.insert_cell()
builder.write('Row 2, Cell 2.')
builder.end_table()
doc.save(file_name=ARTIFACTS_DIR + 'DocumentBuilder.CreateTable.docx')

See Also