The direct use of enumerable business objects as a data source to merge templates is one of the most commonly requested features of TX Text Control Reporting.

In TX Text Control X10, DocumentServer.MailMerge is getting a new method to start the merge process with an IEnumerable object.

public void MergeObjects(System.Collections.IEnumerable mergeData);

Use IEnumerable Objects As Data Sources

MailMerge interprets all public properties of objects in the collection as table columns and child tables. Properties of type IEnumerable are automatically instantiated as relations and are used for merge blocks and nested merge blocks.

Consider the following class structure as a data source:

Using Objects as Data Sources

Below is the C# code that represents the above UML class design:

public class Invoice
{
    public List<Product> Products
    {
        get;
        set;
    }

    public Customer Customer
    {
        get;
        set;
    }
}

public class Product
{
    public Product(string Name, Decimal Price)
    {
        this.Name = Name;
        this.Price = Price;
    }

    public string Name
    {
        get;
        set;
    }

    public Decimal Price
    {
        get;
        set;
    }
}

The template consists of a merge block named Products. The merge fields in the block are named Name and Price.

Using Objects as Data Sources

The following code shows how to create the data source object and how to start the merge process using MergeObjects:

Invoice invoice = new Invoice();

invoice.Products = new List<Product>();
invoice.Products.Add(new Product("Apple", 3.55m));
invoice.Products.Add(new Product("Banana", 2.4m));
invoice.Products.Add(new Product("Pineapple", 2.99m));

var invoices = new List<Invoice>();
invoices.Add(invoice);

mailMerge1.MergeObjects(invoices);

New String Formatter

This sample shows another new feature of TX Text Control X10: Merge field string formatter. The field Price is formatted as a currency string with the format $#,###.00 which renders the results with a currency symbol and 2 decimal places:

Input: 2.4m

Output: $2.40

Using Objects as Data Sources

Define the Textual Representation in Your Classes

TX Text Control X10 Reporting allows the textual formatting to be done in your business objects. You can easily override the ToString() method to return a well formatted address block directly from your business object.

Consider a class to represent a Customer:

Using Objects as Data Sources

The following code is the C# implementation of the above class:

public class Customer
{
    public Customer(string firstName, string lastName, string address, string phone)
    {
        FirstName = firstName;
        LastName = lastName;
        Address = address;
        Phone = phone;
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string Phone { get; set; }

    public override string ToString()
    {
        return String.Format("Customer Details: \nFirst Name - {0} \nLast Name - {1} \nAddress - {2} \nPhone - {3}", FirstName, LastName, Address, Phone);
    }
}

TX Text Control MailMerge calls the ToString() method to get the text from your class which returns a pre-formatted string with carriage returns. The following screenshot shows the results of the above-described new concepts:

Using Objects as Data Sources

These are just some of the great improvements of the upcoming version TX Text Control X10 - stay tuned for more.