
The source code is contained in the following directory:
Used TX Text Control controls:
Relevant API links:
This sample shows how to create invoices using the DocumentServer.MailMerge component. A fictional phone bill is the subject of this sample. A print-ready invoice is created based on a template in the MS Word DOCX format and two XML databases:
A phone bill consists of personal data like the phone number, name or address and universally valid data for all invoices. Additionally, a phone bill contains connection data for each telephone connection. The following illustration shows the structure of the template. The colors are described in the legend.


The DocumentServer.MailMerge.Merge method expects a valid DataSet where the column names matches the names of the merge fields in the template. In the sample, the customer data is stored in the customers.xml database. The following code snippet shows how a DataSet is filled with the database content. Additionally, the missing field names is added as columns to the DataTable:
[C#]
DataSet custData = new DataSet();
custData.ReadXml(Server.MapPath("customers.xml"));
CustomerData = custData.Tables["customer"];
CustomerData.Columns.Add("date");
CustomerData.Columns.Add("service_phone");
CustomerData.Columns.Add("service_fax");
CustomerData.Columns.Add("month");
CustomerData.Columns.Add("tax");
CustomerData.Columns.Add("total_net");
CustomerData.Columns.Add("tax_value");
CustomerData.Columns.Add("total");
[Visual Basic]
DataSet custData = new DataSet()
custData.ReadXml(Server.MapPath("customers.xml"))
CustomerData = custData.Tables("customer");
CustomerData.Columns.Add("date")
CustomerData.Columns.Add("service_phone")
CustomerData.Columns.Add("service_fax")
CustomerData.Columns.Add("month")
CustomerData.Columns.Add("tax")
CustomerData.Columns.Add("total_net")
CustomerData.Columns.Add("tax_value")
CustomerData.Columns.Add("total")
The customer data is displayed in a grid view control on the website. The user is able to select a specific data row for which the invoice should be created for. If no data row is selected, all invoices for all customers will be created.

Select a data row in the data grid by clicking the Select link and confirm with Create Invoice.
In the button's click event, the template is loaded into the DocumentServer.MailMerge instance:
[C#]
if (GridView1.SelectedIndex != -1)
{
object[] selectedRowData = CustomerData.Rows[GridView1.SelectedIndex].ItemArray;
CustomerData.Rows.Clear();
DataRow selectedRow = CustomerData.NewRow();
selectedRow.ItemArray = selectedRowData;
CustomerData.Rows.Add(selectedRow);
}
mailMerge1.LoadTemplate(Server.MapPath("invoice_template.docx"), TXTextControl.DocumentServer.FileFormat.WordprocessingML);
BuildBusinessObject(0);
mailMerge1.Merge(CustomerData, false);
[Visual Basic]
If GridView1.SelectedIndex <> -1 Then
Dim selectedRowData As Object() = CustomerData.Rows(GridView1.SelectedIndex).ItemArray
CustomerData.Rows.Clear()
Dim selectedRow As DataRow = CustomerData.NewRow()
selectedRow.ItemArray = selectedRowData
CustomerData.Rows.Add(selectedRow)
End If
mailMerge1.LoadTemplate(Server.MapPath("invoice_template.docx"), TXTextControl.DocumentServer.FileFormat.WordprocessingML)
BuildBusinessObject(0)
mailMerge1.Merge(CustomerData, False)
In the BuildBusinessObject method, the universal data and the calculated data is computed and added to the CustomerData DataSet. Additionally, the connection data from the connections.xml database is loaded into a DataSet and passed to the DocumentServer.MailMerge.MergeBlocks method:
[C#]
ConnectionData = new DataSet();
ConnectionData.ReadXml(Server.MapPath("connections.xml"));
DataRow[] deleteRows = ConnectionData.Tables["connection"].Select("phone <> '" + CustomerData.Rows[Row]["phone"].ToString() + "'");
foreach (DataRow curRow in deleteRows)
{
ConnectionData.Tables["connection"].Rows.Remove(curRow);
}
mailMerge1.MergeBlocks(ConnectionData);
[Visual Basic]
ConnectionData = New DataSet()
ConnectionData.ReadXml(Server.MapPath("connections.xml"))
Dim deleteRows As DataRow() = ConnectionData.Tables("connection").[Select]("phone <> '" + CustomerData.Rows(Row)("phone").ToString() + "'")
For Each curRow As DataRow In deleteRows
ConnectionData.Tables("connection").Rows.Remove(curRow)
Next
mailMerge1.MergeBlocks(ConnectionData)
The DocumentServer.MailMerge.Merge method creates an invoice with the individual connection data for each data row in the CustomerData DataTable or the currently selected data row.