Skip to content

Commit 36b0f04

Browse files
Added the sample Group-mail-merge-with-data-table
1 parent ff91884 commit 36b0f04

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-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+
}
Loading
Loading
Binary file not shown.

0 commit comments

Comments
 (0)