Skip to content

Commit 653fb49

Browse files
Merge pull request #1 from DharanyaSakthivel-SF4210/master
ES-957426 Add the sample Group-mail-merge-with-data-table
2 parents ff91884 + a05815e commit 653fb49

File tree

7 files changed

+234
-0
lines changed

7 files changed

+234
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
public class Employee
2+
{
3+
private String _firstName;
4+
private String _lastName;
5+
private String _address;
6+
private String _city;
7+
private String _region;
8+
private String _country;
9+
private String _title;
10+
private String _photo;
11+
public String getFirstName()throws Exception
12+
{
13+
return _firstName;
14+
}
15+
public String setFirstName(String value)throws Exception
16+
{
17+
_firstName=value;
18+
return value;
19+
}
20+
public String getLastName()throws Exception
21+
{
22+
return _lastName;
23+
}
24+
public String setLastName(String value)throws Exception
25+
{
26+
_lastName=value;
27+
return value;
28+
}
29+
public String getAddress()throws Exception
30+
{
31+
return _address;
32+
}
33+
public String setAddress(String value)throws Exception
34+
{
35+
_address=value;
36+
return value;
37+
}
38+
public String getCity()throws Exception
39+
{
40+
return _city;
41+
}
42+
public String setCity(String value)throws Exception
43+
{
44+
_city=value;
45+
return value;
46+
}
47+
public String getRegion()throws Exception
48+
{
49+
return _region;
50+
}
51+
public String setRegion(String value)throws Exception
52+
{
53+
_region=value;
54+
return value;
55+
}
56+
public String getCountry()throws Exception{
57+
return _country;
58+
}
59+
public String setCountry(String value)throws Exception
60+
{
61+
_country=value;
62+
return value;
63+
}
64+
public String getTitle()throws Exception
65+
{
66+
return _title;
67+
}
68+
public String setTitle(String value)throws Exception
69+
{
70+
_title=value;
71+
return value;
72+
}
73+
public String getPhoto()throws Exception
74+
{
75+
return _photo;
76+
}
77+
public String setPhoto(String image)throws Exception
78+
{
79+
_photo=image;
80+
return image;
81+
}
82+
public Employee(String firstName,String lastName,String title,String address,String city,String region,String country,String photoFilePath)throws Exception
83+
{
84+
setFirstName(firstName);
85+
setLastName(lastName);
86+
setTitle(title);
87+
setAddress(address);
88+
setCity(city);
89+
setRegion(region);
90+
setCountry(country);
91+
setPhoto((photoFilePath));
92+
}
93+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import java.io.ByteArrayInputStream;
2+
import com.syncfusion.docio.MailMergeDataTable;
3+
import com.syncfusion.docio.MergeImageFieldEventArgs;
4+
import com.syncfusion.docio.MergeImageFieldEventHandler;
5+
import com.syncfusion.docio.WordDocument;
6+
import com.syncfusion.javahelper.system.collections.generic.ListSupport;
7+
import com.syncfusion.javahelper.system.io.FileAccess;
8+
import com.syncfusion.javahelper.system.io.FileMode;
9+
import com.syncfusion.javahelper.system.io.FileStreamSupport;
10+
11+
public class Program {
12+
public static void main(String[] args) throws Exception
13+
{
14+
//Loads an existing Word document into DocIO instance.
15+
WordDocument document = new WordDocument("Template.docx");
16+
//Gets the employee details as IEnumerable collection.
17+
ListSupport<Employee> employeeList = getEmployees();
18+
//Uses the mail merge events handler for image fields.
19+
document.getMailMerge().MergeImageField.add("mergeField_EmployeeImage", new MergeImageFieldEventHandler() {
20+
ListSupport<MergeImageFieldEventHandler> delegateList = new ListSupport<MergeImageFieldEventHandler>(
21+
MergeImageFieldEventHandler.class);
22+
//Represents event handling for MergeFieldEventHandlerCollection.
23+
public void invoke(Object sender, MergeImageFieldEventArgs args) throws Exception
24+
{
25+
mergeField_EmployeeImage(sender, args);
26+
}
27+
//Represents the method that handles MergeField event.
28+
public void dynamicInvoke(Object... args) throws Exception
29+
{
30+
mergeField_EmployeeImage((Object) args[0], (MergeImageFieldEventArgs) args[1]);
31+
}
32+
//Represents the method that handles MergeField event to add collection item.
33+
public void add(MergeImageFieldEventHandler delegate) throws Exception
34+
{
35+
if (delegate != null)
36+
delegateList.add(delegate);
37+
}
38+
//Represents the method that handles MergeField event to remove collection item.
39+
public void remove(MergeImageFieldEventHandler delegate) throws Exception
40+
{
41+
if (delegate != null)
42+
delegateList.remove(delegate);
43+
}
44+
});
45+
//Creates an instance of MailMergeDataTable by specifying MailMerge group name and IEnumerable collection.
46+
MailMergeDataTable dataSource = new MailMergeDataTable("Employees",employeeList);
47+
//Executes the mail merge for group.
48+
document.getMailMerge().executeGroup(dataSource);
49+
//Saves and closes the WordDocument instance.
50+
document.save("Sample.docx");
51+
document.close();
52+
}
53+
public static ListSupport<Employee> getEmployees()throws Exception
54+
{
55+
ListSupport<Employee> employees = new ListSupport<Employee>(Employee.class);
56+
employees.add(new Employee("Nancy","Smith","Sales Representative","505 - 20th Ave. E. Apt. 2A,","Seattle","WA","USA","Nancy.png"));
57+
employees.add(new Employee("Andrew","Fuller","Vice President, Sales","908 W. Capital Way","Tacoma","WA","USA","Andrew.png"));
58+
return employees;
59+
}
60+
61+
public static void mergeField_EmployeeImage(Object sender, MergeImageFieldEventArgs args) throws Exception
62+
{
63+
//Binds image from file system during mail merge.
64+
if ((args.getFieldName()).equals("Photo"))
65+
{
66+
String ProductFileName = args.getFieldValue().toString();
67+
//Gets the image from file system.
68+
FileStreamSupport imageStream = new FileStreamSupport(ProductFileName, FileMode.Open, FileAccess.Read);
69+
ByteArrayInputStream stream = new ByteArrayInputStream(imageStream.toArray());
70+
args.setImageStream(stream);
71+
}
72+
}
73+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import com.syncfusion.docio.*;
2+
import com.syncfusion.javahelper.system.collections.generic.DictionarySupport;
3+
import com.syncfusion.javahelper.system.collections.generic.ListSupport;
4+
import com.google.gson.Gson;
5+
import com.google.gson.JsonArray;
6+
import com.google.gson.JsonObject;
7+
8+
public class Program {
9+
public static void main(String[] args) throws Exception {
10+
// JSON string containing nested organizational data
11+
String json = "{\"Organizations\":[{\"BranchName\":\"UK Office\",\"Address\":\"120 Hanover Sq.\",\"City\":\"London\",\"ZipCode\":\"WX1 6LT\",\"Country\":\"UK\",\"Departments\":[{\"DepartmentName\":\"Marketing\",\"Supervisor\":\"Nancy Davolio\",\"Employees\":[{\"EmployeeName\":\"Thomas Hardy\",\"EmployeeID\":\"1001\",\"JoinedDate\":\"05/27/1996\"},{\"EmployeeName\":\"Maria Anders\",\"EmployeeID\":\"1002\",\"JoinedDate\":\"04/10/1998\"}]},{\"DepartmentName\":\"Production\",\"Supervisor\":\"Andrew Fuller\",\"Employees\":[{\"EmployeeName\":\"Elizabeth Lincoln\",\"EmployeeID\":\"1003\",\"JoinedDate\":\"05/15/1996\"},{\"EmployeeName\":\"Antonio Moreno\",\"EmployeeID\":\"1004\",\"JoinedDate\":\"04/22/1996\"}]}]}]}";
12+
13+
// Convert JSON string into a JsonObject
14+
JsonObject data = new Gson().fromJson(json, JsonObject.class);
15+
16+
// Convert JsonObject to DictionarySupport (required format for mail merge)
17+
DictionarySupport<String, Object> result = getData(data);
18+
19+
// Load the Word document template
20+
WordDocument document = new WordDocument("Template.docx");
21+
22+
// Extract the list of organizations for mail merge
23+
MailMergeDataTable dataTable = new MailMergeDataTable("Organizations",
24+
(ListSupport<Object>) result.get("Organizations"));
25+
26+
// Perform nested mail merge with the data table
27+
document.getMailMerge().executeNestedGroup(dataTable);
28+
29+
// Save the generated Word document
30+
document.save("Result.docx", FormatType.Docx);
31+
document.close();
32+
33+
System.out.println("Word document generated successfully.");
34+
}
35+
36+
// Converts a JsonObject into a DictionarySupport for Syncfusion mail merge
37+
public static DictionarySupport<String, Object> getData(JsonObject data) throws Exception {
38+
DictionarySupport<String, Object> map = new DictionarySupport<>(String.class, Object.class);
39+
40+
for (String key : data.keySet()) {
41+
Object keyValue = null;
42+
if (data.get(key).isJsonArray()) {
43+
// Convert array to ListSupport
44+
keyValue = getData(data.getAsJsonArray(key));
45+
} else if (data.get(key).isJsonPrimitive()) {
46+
// Use primitive value directly
47+
keyValue = data.get(key).getAsString();
48+
}
49+
map.add(key, keyValue);
50+
}
51+
return map;
52+
}
53+
54+
// Converts a JsonArray into ListSupport recursively
55+
public static ListSupport<Object> getData(JsonArray arr) throws Exception {
56+
ListSupport<Object> list = new ListSupport<>(Object.class);
57+
58+
for (int i = 0; i < arr.size(); i++) {
59+
Object keyValue = null;
60+
if (arr.get(i).isJsonObject()) {
61+
// Recursively convert nested objects
62+
keyValue = getData(arr.get(i).getAsJsonObject());
63+
}
64+
list.add(keyValue);
65+
}
66+
return list;
67+
}
68+
}
Loading
Loading
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)