Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions examples/mailmerge/Group-mail-merge-with-data-table/Employees.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
public class Employee
{
private String _firstName;
private String _lastName;
private String _address;
private String _city;
private String _region;
private String _country;
private String _title;
private String _photo;
public String getFirstName()throws Exception
{
return _firstName;
}
public String setFirstName(String value)throws Exception
{
_firstName=value;
return value;
}
public String getLastName()throws Exception
{
return _lastName;
}
public String setLastName(String value)throws Exception
{
_lastName=value;
return value;
}
public String getAddress()throws Exception
{
return _address;
}
public String setAddress(String value)throws Exception
{
_address=value;
return value;
}
public String getCity()throws Exception
{
return _city;
}
public String setCity(String value)throws Exception
{
_city=value;
return value;
}
public String getRegion()throws Exception
{
return _region;
}
public String setRegion(String value)throws Exception
{
_region=value;
return value;
}
public String getCountry()throws Exception{
return _country;
}
public String setCountry(String value)throws Exception
{
_country=value;
return value;
}
public String getTitle()throws Exception
{
return _title;
}
public String setTitle(String value)throws Exception
{
_title=value;
return value;
}
public String getPhoto()throws Exception
{
return _photo;
}
public String setPhoto(String image)throws Exception
{
_photo=image;
return image;
}
public Employee(String firstName,String lastName,String title,String address,String city,String region,String country,String photoFilePath)throws Exception
{
setFirstName(firstName);
setLastName(lastName);
setTitle(title);
setAddress(address);
setCity(city);
setRegion(region);
setCountry(country);
setPhoto((photoFilePath));
}
}
73 changes: 73 additions & 0 deletions examples/mailmerge/Group-mail-merge-with-data-table/Program.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import java.io.ByteArrayInputStream;
import com.syncfusion.docio.MailMergeDataTable;
import com.syncfusion.docio.MergeImageFieldEventArgs;
import com.syncfusion.docio.MergeImageFieldEventHandler;
import com.syncfusion.docio.WordDocument;
import com.syncfusion.javahelper.system.collections.generic.ListSupport;
import com.syncfusion.javahelper.system.io.FileAccess;
import com.syncfusion.javahelper.system.io.FileMode;
import com.syncfusion.javahelper.system.io.FileStreamSupport;

public class Program {
public static void main(String[] args) throws Exception
{
//Loads an existing Word document into DocIO instance.
WordDocument document = new WordDocument("Template.docx");
//Gets the employee details as IEnumerable collection.
ListSupport<Employee> employeeList = getEmployees();
//Uses the mail merge events handler for image fields.
document.getMailMerge().MergeImageField.add("mergeField_EmployeeImage", new MergeImageFieldEventHandler() {
ListSupport<MergeImageFieldEventHandler> delegateList = new ListSupport<MergeImageFieldEventHandler>(
MergeImageFieldEventHandler.class);
//Represents event handling for MergeFieldEventHandlerCollection.
public void invoke(Object sender, MergeImageFieldEventArgs args) throws Exception
{
mergeField_EmployeeImage(sender, args);
}
//Represents the method that handles MergeField event.
public void dynamicInvoke(Object... args) throws Exception
{
mergeField_EmployeeImage((Object) args[0], (MergeImageFieldEventArgs) args[1]);
}
//Represents the method that handles MergeField event to add collection item.
public void add(MergeImageFieldEventHandler delegate) throws Exception
{
if (delegate != null)
delegateList.add(delegate);
}
//Represents the method that handles MergeField event to remove collection item.
public void remove(MergeImageFieldEventHandler delegate) throws Exception
{
if (delegate != null)
delegateList.remove(delegate);
}
});
//Creates an instance of MailMergeDataTable by specifying MailMerge group name and IEnumerable collection.
MailMergeDataTable dataSource = new MailMergeDataTable("Employees",employeeList);
//Executes the mail merge for group.
document.getMailMerge().executeGroup(dataSource);
//Saves and closes the WordDocument instance.
document.save("Sample.docx");
document.close();
}
public static ListSupport<Employee> getEmployees()throws Exception
{
ListSupport<Employee> employees = new ListSupport<Employee>(Employee.class);
employees.add(new Employee("Nancy","Smith","Sales Representative","505 - 20th Ave. E. Apt. 2A,","Seattle","WA","USA","Nancy.png"));
employees.add(new Employee("Andrew","Fuller","Vice President, Sales","908 W. Capital Way","Tacoma","WA","USA","Andrew.png"));
return employees;
}

public static void mergeField_EmployeeImage(Object sender, MergeImageFieldEventArgs args) throws Exception
{
//Binds image from file system during mail merge.
if ((args.getFieldName()).equals("Photo"))
{
String ProductFileName = args.getFieldValue().toString();
//Gets the image from file system.
FileStreamSupport imageStream = new FileStreamSupport(ProductFileName, FileMode.Open, FileAccess.Read);
ByteArrayInputStream stream = new ByteArrayInputStream(imageStream.toArray());
args.setImageStream(stream);
}
}
}
68 changes: 68 additions & 0 deletions examples/mailmerge/NestedGroup-MailMerge-with-JSON/Program.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import com.syncfusion.docio.*;
import com.syncfusion.javahelper.system.collections.generic.DictionarySupport;
import com.syncfusion.javahelper.system.collections.generic.ListSupport;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class Program {
public static void main(String[] args) throws Exception {
// JSON string containing nested organizational data
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\"}]}]}]}";

// Convert JSON string into a JsonObject
JsonObject data = new Gson().fromJson(json, JsonObject.class);

// Convert JsonObject to DictionarySupport (required format for mail merge)
DictionarySupport<String, Object> result = getData(data);

// Load the Word document template
WordDocument document = new WordDocument("Template.docx");

// Extract the list of organizations for mail merge
MailMergeDataTable dataTable = new MailMergeDataTable("Organizations",
(ListSupport<Object>) result.get("Organizations"));

// Perform nested mail merge with the data table
document.getMailMerge().executeNestedGroup(dataTable);

// Save the generated Word document
document.save("Result.docx", FormatType.Docx);
document.close();

System.out.println("Word document generated successfully.");
}

// Converts a JsonObject into a DictionarySupport for Syncfusion mail merge
public static DictionarySupport<String, Object> getData(JsonObject data) throws Exception {
DictionarySupport<String, Object> map = new DictionarySupport<>(String.class, Object.class);

for (String key : data.keySet()) {
Object keyValue = null;
if (data.get(key).isJsonArray()) {
// Convert array to ListSupport
keyValue = getData(data.getAsJsonArray(key));
} else if (data.get(key).isJsonPrimitive()) {
// Use primitive value directly
keyValue = data.get(key).getAsString();
}
map.add(key, keyValue);
}
return map;
}

// Converts a JsonArray into ListSupport recursively
public static ListSupport<Object> getData(JsonArray arr) throws Exception {
ListSupport<Object> list = new ListSupport<>(Object.class);

for (int i = 0; i < arr.size(); i++) {
Object keyValue = null;
if (arr.get(i).isJsonObject()) {
// Recursively convert nested objects
keyValue = getData(arr.get(i).getAsJsonObject());
}
list.add(keyValue);
}
return list;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.