Hello Aspose Team,
We are creating a POC to evaluate Aspose and other library to build a solution arround mailmerge.
Please find attached input and output template with the working case.
User Name : -«tablestart:Users»«Name»
Department Name : - «tablestart:Departments»«Name»
Addresss:- «tablestart:Addreses»
Name :-«Name»
Street :- «Street»
«tableend:Addreses»
«tableend:Departments»
«tableend:Users»
And Custom IMailMergeDataSource Source indent code is as follows :
import com.aspose.words.IMailMergeDataSource;
import java.util.List;
public class UserMailMergeDataSource implements IMailMergeDataSource {
public UserMailMergeDataSource(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)
{
System.out.println("Field Name::Custom User :::::" + fieldName);
fieldValue[0] = getFieldValue(fieldName);
return fieldValue[0] != null;
}
public IMailMergeDataSource getChildDataSource(String childName) throws Exception {
System.out.println("childName:" + childName);
Object childData = getFieldValue(childName);
System.out.println("childData::" + childData);
if (childData != null && List.class.isInstance(childData)) {
// List departments = (List) getFieldValue(childName);
return new UserMailMergeDataSource((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.
// System.out.println("mRecordIndex::::::" + mRecordIndex);
Object item = mItems.get(mRecordIndex);
Class itemClass = item.getClass();
// System.out.println("item:::class:::" + item.getClass().getName());
java.lang.reflect.Field field = null;
try {
field = itemClass.getField(fieldName);
// field.get(itemClass.getName().equals())
System.out.println("field::::::" + field);
} 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;
}
Using following Pojo:-
- User.Java
public class User
{
public List Departments = new ArrayList();
public List getDepartments() {
return Departments;
}
public void setDepartments(List departments) {
Departments = departments;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String Name;
}
- Department.java
public class Department
{
public List Addreses = new ArrayList();
public List getAddreses() {
return Addreses;
}
public void setAddreses(List addreses) {
Addreses = addreses;
}
public String Name;
public void setName(String name) {
Name = name;
}
}
- Address.java
public class Address
{
public String Name;
public String Street;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getStreet() {
return Street;
}
public void setStreet(String street) {
Street = street;
}
}
How can we implement custom code using following template:-
«user[0].department[3].address[0].name»
*We are able to accomplish most of the feature that we need in POC but following use case not seems to be working with input template.
«user[0].department[3].address[0].name»*
Could anyone in your team let us know that, Is IMailMergeDataSource really supported with above format to populate the data , if yes then can you please provide the sample code ?
Thanks in advance.
Regards,
Dilip