Basically I have a Array list in java and now I need to send that list to mailmerge doc to create a repeated data.
ArrayList items = new ArrayList();
for(int i=1; i<5; i++)
{
items.add("List Item " + i);
}
// Open tempalte
Document doc = new Document(MyDir + "in.docx");
// Create a datasource.
MailMergeDataSource data = new MailMergeDataSource(items, "items");
// Execute mail merge with regions.
doc.getMailMerge().executeWithRegions(data);
// Save output.
doc.save(MyDir + "Out.docx");
--------------------------------------------------
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] = mItems.get(mRecordIndex);
return fieldValue[0] != null;
}
public IMailMergeDataSource getChildDataSource(String childName) throws Exception {
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 ArrayList mItems;
private int mRecordIndex;
private String mTableName;
}