We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Mail Merge using Java Objects

Hi,


How to use Mail Merge using Java Objects? Do you have any example to refer?

Scenario: I have a Claim object which has list of Party object. I like to display the properties of Claim in the header and properties of Party list in a table below header.
Hi Trinadh,

Thanks for your inquiry. We suggest you please implement IMailMergeDataSource interface to allow mail merge from a custom data source, such as a list of objects. Master-detail data is also supported.

When a data source is created, it should be initialized to point to BOF (before the first record). The Aspose.Words mail merge engine will invoke moveNext() to advance to next record and then invoke getValue(java.lang.String,java.lang.Object[]) for every merge field it encounters in the document or the current mail merge region.

The Aspose.Words mail merge engine invokes getChildDataSource method when it encounters a beginning of a nested mail merge region. When the Aspose.Words mail merge engines populates a mail merge region with data and encounters the beginning of a nested mail merge region in the form of MERGEFIELD TableStart:TableName, it invokes getChildDataSource(java.lang.String) on the current data source object. Your implementation needs to return a new data source object that will provide access to the child records of the current parent record. Aspose.Words will use the returned data source to populate the nested mail merge region.

Please check following code example for your kind reference. Hope this helps you.

public void Test001() throws Exception {

// Prepare data.

ArrayList parents = new ArrayList();

for(int i=0; i<5; i++)

{

Parent parent = new Parent(String.format("Parent_#%d", i));

for(int j=0; j<2; j++)

parent.Children.add(new Child(String.format("Child_#%d_%d", i, j)));

parents.add(parent);

}

// Open tempalte

Document doc = new Document(MyDir + "in.docx");

// Create a datasource.

MailMergeDataSource ds = new MailMergeDataSource(parents, "Parents");

// Execute mail merge with regions.

doc.getMailMerge().executeWithRegions(ds);

// Save output.

doc.save(MyDir + "Out.docx");

}

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;

}

class MailMergeDataSource implements IMailMergeDataSource{

public MailMergeDataSource(ArrayList 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 && ArrayList.class.isInstance(childData))

return new MailMergeDataSource((ArrayList)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 ArrayList mItems;

private int mRecordIndex;

private String mTableName;

}