Nested Mail Merge - Is IMailMergeDataSource really supported?

Hi,

We need to use nested mail merge with IMailMergeDataSource, as DataSet doesn’t fit into our system. We tried Aspose Words for Java 13.5.0 (with temporary license) on two of our products, both are web applications, and they simply don’t work: the mail merge fields are not merged at all, and no errors threw in the system.

Please see attached mail merge file and relevant java source codes. Please note that fields in the outer table Employee are resolved by display name in the system, while the fields in the inner table ReviewHistory are resolved by field name in the java class.

Could anyone in your team let us know that, Is IMailMergeDataSource really supported in nested mail merge? If the answer is yes, could anyone tell us what might be wrong in the attached files?

Thanks very much.

Mark

Hi Mark,

Thanks for your inquiry. You can find sample code for implementing IMailMergeDataSource with a child data source in the following article. Please read the section 'Creating Relations between IMailMergeDataSource Objects’.
https://docs.aspose.com/words/java/nested-mail-merge-with-regions/

Hope this helps you.

Hi Mark,

The following code example use IMailMergeDataSource for nested mail merge regions. Please find the sample input and output documents in attachment. Hope this helps you. Please let us know if you have any more queries.

public void Test001() throws Exception
{
    // Prepare data.
    ArrayList parents = new ArrayList();
    for (int i = 0; i <10; i++)
    {
        Parent parent = new Parent(String.format("Parent_#%d", i));
        for (int j = 0; j <5; j++)
            parent.Children.add(new Child(String.format("Child_#%d_%d", i, j)));
        parents.add(parent);
    }
    // Open tempalte
    Document doc = new Document("C:\Temp\in.doc");
    // Create a datasource.
    MailMergeDataSource ds = new MailMergeDataSource(parents, "Parents");
    // Execute mail merge with regions.
    doc.getMailMerge().executeWithRegions(ds);
    // Save output.
    doc.save("C:\Temp\out.doc");
}
private static class Parent
{
    public Parent(String name)
    {
        Name = name;
        Children = new ArrayList();
    }
    public final ArrayList Children;
    public final String Name;
}
private static class Child
{
    public Child(String name)
    {
        Name = name;
    }
    public final String Name;
}
private class MailMergeDataSource implements IMailMergeDataSource
{
    public MailMergeDataSource(List items, String tableName)
    {
        mItems = items;
        mTableName = tableName;
        // When the data source is initialized, it must be positioned before the first record.
        mRecordIndex = -1;
    }
    ///

    /// The name of the data source. Used by Aspose.Words only when executing mail merge with repeatable regions.
    ///
    public String getTableName()
    {
        return mTableName;
    }
    ///

    /// Aspose.Words call this to get a value for every data field.
    ///
    public boolean getValue(String fieldName, Object[] fieldValue)
    {
        fieldValue[0] = getFieldValue(fieldName);
        return fieldValue[0] != null;
    }
    public IMailMergeDataSource getChildDataSource(String childName) throws Exception
    {
        Object childData = getFieldValue(childName);
        if (childData != null && List.class.isInstance(childData))
            return new MailMergeDataSource((List) childData, childName);
        return null;
    }
    ///

    /// A standard implementation for moving to a next record in a collection.
    ///
    public boolean moveNext()
    {
        if (isEof())
            return false;
        mRecordIndex++;
        return (!isEof());
    }
    private boolean isEof()
    {
        return (mRecordIndex>= mItems.size());
    }
    private Object getFieldValue(String fieldName)
    {
        // Get value of the appropriate field usign reflection.
        Object item = mItems.get(mRecordIndex);
        Class itemClass = item.getClass();
        java.lang.reflect.Field field = null;
        try
        {
            field = itemClass.getField(fieldName);
        }
        catch (Exception ex)
        {}
        if (field != null)
        {
            try
            {
                return field.get(item);
            }
            catch (Exception ex)
            {}
            return true;
        }
        return null;
    }
    private List mItems;
    private int mRecordIndex;
    private String mTableName;
}

Hi Tahir,

I was calling getMailMerge().execute(), while I need to call getMailMerge().executeWithRegions() - that’s all the issue.

Many thanks for the help.

Mark

Hi Mark,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.