forked from SyncfusionExamples/Java-Word-Table-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordTable.java
More file actions
149 lines (145 loc) · 4.68 KB
/
WordTable.java
File metadata and controls
149 lines (145 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import java.io.*;
import com.syncfusion.docio.*;
import com.syncfusion.javahelper.system.*;
import com.syncfusion.javahelper.system.collections.generic.ListSupport;
import com.syncfusion.javahelper.system.io.*;
import com.syncfusion.javahelper.system.xml.*;
public class WordTable{
public static void main(String[] args) throws Exception {
// Loads the template document.
WordDocument document = new WordDocument(getDataDir("WordTable_Template.docx"));
// Creates a list of employee details.
ListSupport<Employees> employeeDetails = getEmployeeDetails();
// Iterates each item in the list.
for (Employees employee : employeeDetails)
{
// Accesses the table in the document.
IWTable table = document.getSections().get(0).getTables().get(0);
// Initializes the paragraph and add new row to the table.
IWParagraph paragraph = null;
WTableRow newRow = null;
newRow = table.addRow();
// Gets the employee photo and convert that base64 string to bytes.
byte[] bytes = ConvertSupport.fromBase64String(employee.getPhoto());
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
int index = table.getRows().getCount();
// Appends the picture to the first cell.
table.getRows().get(index - 1).getCells().get(0).addParagraph().appendPicture(stream);
// Appends the employee details in the second cell.
paragraph = table.getRows().get(index - 1).getCells().get(1).addParagraph();
paragraph.appendText(employee.getName() + "\n" + employee.getTitle() + "\n" + employee.getAddress() + "\n"
+ employee.getHomePhone());
}
// Saves and closes the document.
document.save("Result.docx");
document.close();
}
/**
*
* Gets the list of employee details.
*
*/
private static ListSupport<Employees> getEmployeeDetails() throws Exception {
// Gets list of employee details.
ListSupport<Employees> employees = new ListSupport<Employees>(Employees.class);
// Reads the xml document.
FileStreamSupport fs = new FileStreamSupport(getDataDir("EmployeesList.xml"), FileMode.Open, FileAccess.Read);
XmlReaderSupport reader = XmlReaderSupport.create(fs);
if (reader == null)
throw new Exception("reader");
while (reader.getNodeType() != XmlNodeType.Element)
reader.read();
if (reader.getLocalName() != "Employees")
throw new Exception(StringSupport.concat("Unexpected xml tag ", reader.getLocalName()));
reader.read();
while (reader.getNodeType() == XmlNodeType.Whitespace)
reader.read();
// Iterates to add the employee details in list.
while (reader.getLocalName() != "Employees")
{
if (reader.getNodeType() == XmlNodeType.Element)
{
switch (reader.getLocalName())
{
case "Employee":
employees.add(getEmployees(reader));
break;
}
}
else
{
reader.read();
if ((reader.getLocalName() == "Employees") && reader.getNodeType() == XmlNodeType.EndElement)
break;
}
}
return employees;
}
/**
*
* Gets the employees.
*
* @param reader Syncfusion's XML reader to read the XML files..
*/
private static Employees getEmployees(XmlReaderSupport reader) throws Exception {
if (reader == null)
throw new Exception("reader");
while (reader.getNodeType() != XmlNodeType.Element)
reader.read();
if (reader.getLocalName() != "Employee")
throw new Exception(StringSupport.concat("Unexpected xml tag ", reader.getLocalName()));
reader.read();
while (reader.getNodeType() == XmlNodeType.Whitespace)
reader.read();
Employees employee = new Employees();
while (reader.getLocalName() != "Employee")
{
if (reader.getNodeType() == XmlNodeType.Element)
{
switch (reader.getLocalName())
{
case "Name":
employee.setName(reader.readContentAsString());
break;
case "Title":
employee.setTitle(reader.readContentAsString());
break;
case "Address":
employee.setAddress(reader.readContentAsString());
break;
case "HomePhone":
employee.setHomePhone(reader.readContentAsString());
break;
case "Photo":
employee.setPhoto(reader.readContentAsString());
break;
default:
reader.skip();
break;
}
}
else
{
reader.read();
if ((reader.getLocalName() == "Employee") && reader.getNodeType() == XmlNodeType.EndElement)
break;
}
}
return employee;
}
/**
* Get the file path
*
* @param path specifies the file path
*/
private static String getDataDir(String path) {
File dir = new File(System.getProperty("user.dir"));
if (!(dir.toString().endsWith("Java-Word-Table-Examples")))
dir = dir.getParentFile();
dir = new File(dir, "resources");
dir = new File(dir, path);
if (dir.isDirectory() == false)
dir.mkdir();
return dir.toString();
}
}