Skip to content

Commit a05815e

Browse files
Added the sample NestedGroup-MailMerge-with-JSON
1 parent 36b0f04 commit a05815e

File tree

2 files changed

+68
-0
lines changed
  • examples/mailmerge/NestedGroup-MailMerge-with-JSON
  • resources/mailmerge/NestedGroup-MailMerge-with-JSON

2 files changed

+68
-0
lines changed
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+
}
Binary file not shown.

0 commit comments

Comments
 (0)