|
| 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 | +} |
0 commit comments