Use DocumentBuilder to Insert Document Elements
The DocumentBuilder
is used to modify documents. This article explains and describes how to perform a number of tasks:
Inserting a String of Text
Simply pass the string of text you need to insert into the document to the DocumentBuilder.Write
method. Text formatting is determined by the Font
property. This object contains different font attributes (font name, font size, colour, and so on). Some important font attributes are also represented by DocumentBuilder properties to allow you to access them directly. These are Boolean properties Font.Bold
, Font.Italic
, and Font.Underline
.
Note that the character formatting you set will apply to all text inserted from the current position in the document onwards.
Below example Inserts formatted text using DocumentBuilder.
Inserting a Paragraph
DocumentBuilder.Writeln
inserts a string of text into the document as well but in addition, it adds a paragraph break. Current font formatting is also specified by the DocumentBuilder.Font
property and current paragraph formatting is determined by the DocumentBuilder.ParagraphFormat
property. Below example shows how to insert a paragraph into the document.
Inserting a Table
The basic algorithm for creating a table using DocumentBuilder is simple:
- Start the table using
DocumentBuilder.StartTable
. - Insert a cell using
DocumentBuilder.InsertCell
. This automatically starts a new row. If needed, use theDocumentBuilder.CellFormat
property to specify cell formatting. - Insert cell contents using the
DocumentBuilder
methods. - Repeat steps 2 and 3 until the row is complete.
- Call
DocumentBuilder.EndRow
to end the current row. If needed, useDocumentBuilder.RowFormat
property to specify row formatting. - Repeat steps 2 - 5 until the table is complete.
- Call
DocumentBuilder.EndTable
to finish the table building. The appropriate DocumentBuilder table creation methods are described below.
Starting a Table
Calling DocumentBuilder.StartTable
is the first step in building a table. It can be also called inside a cell, in which case it starts a nested table. The next method to call is DocumentBuilder.InsertCell
.
Inserting a Cell
After you call DocumentBuilder->InsertCell
, a new cell is created and any content you add using other methods of the DocumentBuilder
class will be added to the current cell. To start a new cell in the same row, call DocumentBuilder->InsertCell
again. Use the DocumentBuilder.CellFormat
property to specify cell formatting. It returns a CellFormat
object that represents all formatting for a table cell.
Ending a Row
Call DocumentBuilder.EndRow
to finish the current row. If you call DocumentBuilder->InsertCell
immediately after that, then the table continues on a new row.
Use the DocumentBuilder.RowFormat
property to specify row formatting. It returns a RowFormat
object that represents all formatting for a table row.
Ending a Table
Call DocumentBuilder.EndTable
to finish the current table. This method should be called only once after DocumentBuilder->EndRow
was called. When called, DocumentBuilder.EndTable
moves the cursor out of the current cell to a position just after the table. The following example demonstrates how to build a formatted table that contains 2 rows and 2 columns.
Inserting a Break
If you want to explicitly start a new line, paragraph, column, section, or page, call DocumentBuilder.InsertBreak
. Pass to this method the type of the break you need to insert that is represented by the BreakType
enumeration. Below example shows how to insert page breaks into a document.
Inserting an Image
DocumentBuilder provides several overloads of the DocumentBuilder->InsertImage
method that allows you to insert an inline or floating image. If the image is an EMF or WMF metafile, it will be inserted into the document in metafile format. All other images will be stored in PNG format. The DocumentBuilder->InsertImage
method can use images from different sources:
- From a file or
URL
by passing a string parameterDocumentBuilder->InsertImage
. - From a stream by passing a
Stream
parameterDocumentBuilder->InsertImage
. - From an Image object by passing an Image parameter
DocumentBuilder->InsertImage
. - From a byte array by passing a byte array parameter
DocumentBuilder.InsertImage
.For each of theDocumentBuilder->InsertImage
methods, there are further overloads which allow you to insert an image with the following options: - Inline or floating at a specific position, for example,
DocumentBuilder->InsertImage
. - Percentage scale or custom size, for example,
DocumentBuilder.InsertImage
. Furthermore theDocumentBuilder->InsertImage
method returns aShape
object that was just created and inserted so you can further modify properties of the Shape.
Inserting an Inline Image
Pass a single string representing a file that contains the image to DocumentBuilder->InsertImage
to insert the image into the document as an inline graphics. Below example shows how to insert an inline image at the cursor position into a document.
Inserting a Floating (Absolutely Positioned) Image
This example inserts a floating image from a file or URL
at a specified position and size.
Inserting a Bookmark
To insert a bookmark into the document, you should do the following:
- Call
DocumentBuilder->StartBookmark
passing it the desired name of the bookmark. - Insert the bookmark text using DocumentBuilder methods.
- Call
DocumentBuilder.EndBookmark
passing it the same name that you used with DocumentBuilder->StartBookmark. - Bookmarks can overlap and span any range. To create a valid bookmark you need to call both
DocumentBuilder->StartBookmark
andDocumentBuilder->EndBookmark
with the same bookmark name.
Below example shows how to insert a bookmark into a document using a document builder.
Inserting a Form
Field
Form fields are a particular case of Word fields that allows “interaction” with the user. Form fields in Microsoft Word include textbox, combo box and checkbox.DocumentBuilder provides special methods to insert each type of form field into the document: DocumentBuilder.InsertTextInput
, DocumentBuilder->InsertCheckBox
, and DocumentBuilder.InsertComboBox
. Note that if you specify a name for the form field, then a bookmark is automatically created with the same name.
Inserting a Text Input
DocumentBuilder.InsertTextInput
to insert a textbox into the document. Below example shows how to insert a text input form field into a document.
Inserting a Check Box
Call DocumentBuilder.InsertCheckBox
to insert a checkbox into the document. Below example shows how to insert a checkbox form field into a document.
Inserting a Combo Box
Call DocumentBuilder.InsertComboBox
to insert a combo box into the document. Below example shows how to insert a combo box form field into a document.
Inserting Locale at Field Level
Customers can specify Locale at field level now and can achieve better control. Locale Ids can be associated with each field inside the DocumentBuilder. The examples below illustrate how to make use of this option.
Inserting a Hyperlink
Use DocumentBuilder.InsertHyperlink
to insert a hyperlink into the document. This method accepts three parameters: text of the link to be displayed in the document, link destination (URL or a name of a bookmark inside the document), and a boolean parameter that should be true if the URL
is a name of a bookmark inside the document.DocumentBuilder.InsertHyperlink internally calls DocumentBuilder.InsertField
.The method always adds apostrophes at the beginning and end of the URL. Note that you need to specify font formatting for the hyperlink display text explicitly using the Font
property. Below example inserts a hyperlink into a document using DocumentBuilder.
Inserting Ole Object
If you want Ole Object call DocumentBuilder.InsertOleObject
. Pass to this method the ProgId
explicitly with other parameters. Below example shows how to insert Ole Object into a document.
Set File Name and Extension when Inserting Ole Object
OLE package is a legacy and “undocumented” way to store embedded object if OLE handler is unknown. Early Windows versions such as Windows 3.1, 95 and 98 had Packager.exe application which could be used to embed any type of data into the document. Now, this application is excluded from Windows but MS Word and other applications still use it to embed data if OLE handler is missing or unknown. OlePackage class allows to access OLE Package properties. Below example shows how to set file name, extension and display name for OLE Package.
Inserting HTML
You can easily insert an HTML string that contains an HTML fragment or whole HTML document into the Word document. Just pass this string to the DocumentBuilder->InsertHtml
method. One of the useful implementations of the method is storing an HTML string in a database and inserting it into the document during Mail Merge to get the formatted content added instead of building it using various methods of the document builder. Below example shows inserts HTML into a document using DocumentBuilder.
Insert Horizontal Rule into Document
Below code example shows how to insert horizontal rule shape into a document using DocumentBuilder->InsertHorizontalRule
method.