| JAVA |
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
package com.aspose.words.demos;
import com.aspose.words.DataRelation;
import com.aspose.words.DataSet;
import com.aspose.words.DataTable;
import com.aspose.words.Document;
import javax.faces.el.ValueBinding;
import java.sql.ResultSet;
public class SalesInvoiceDemo extends Demo
{
public SalesInvoiceDemo()
{
}
public Document execute() throws Exception
{
Document doc = new Document(getPathToTemplateDoc());
doc.getMailMerge().setRemoveEmptyParagraphs(true);
doc.getMailMerge().executeWithRegions(getOrdersData());
return doc;
}
private DataSet getOrdersData() throws Exception
{
DataSet dataSet = new DataSet();
DataTable orders = new DataTable(executeQuery("SELECT TOP 3 * FROM AsposeWordOrders"), "Orders");
dataSet.getTables().add(orders);
DataTable orderDetails = new DataTable(executeQuery("SELECT * FROM AsposeWordOrderDetails"), "OrderDetails");
dataSet.getTables().add(orderDetails);
DataTable orderTotals = new DataTable(executeQuery("SELECT * FROM AsposeWordOrderTotals"), "OrderTotals");
dataSet.getTables().add(orderTotals);
dataSet.getRelations().add(new DataRelation(
"OrderToOrderDetails",
"Orders",
"OrderDetails",
new String[]{"OrderID"},
new String[]{"OrderID"}));
dataSet.getRelations().add(new DataRelation(
"OrderToOrderTotals",
"Orders",
"OrderTotals",
new String[]{"OrderID"},
new String[]{"OrderID"}));
return dataSet;
}
public String executeDemo() {
try
{
init("SalesInvoiceDemo");
String formatType = request.getParameter("format");
ValueBinding theBean = context.getApplication().createValueBinding("#{mycontext.outputtype}");
if (null!=theBean)
{
formatType = (String)theBean.getValue(context);
}
boolean openNewWindow = request.getParameter("openNewWindow") != null;
theBean = context.getApplication().createValueBinding("#{mycontext.opennewwindow}");
if (null!=theBean)
{
openNewWindow = (Boolean)theBean.getValue(context);
}
Document doc = execute();
if (null!=doc) {
sendToBrowser(doc, mDemoName, formatType, openNewWindow);
}
}
catch (Exception e)
{
throw new RuntimeException(e);
}
return "";
}
private ResultSet getTestOrder() throws Exception
{
return executeQuery(
"SELECT * FROM AsposeWordOrders WHERE OrderId = " + TestOrderId);
}
private ResultSet getTestOrderDetails() throws Exception
{
return executeQuery(
"SELECT * FROM AsposeWordOrderDetails WHERE OrderId = " + TestOrderId + " ORDER BY ProductID" );
}
private ResultSet getTestOrderTotals() throws Exception
{
return executeQuery(
"SELECT * FROM AsposeWordOrderTotals WHERE OrderId = " + TestOrderId);
}
private final int TestOrderId = 10444;
}
|
| JAVA |
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
|
package com.aspose.words.demos;
import java.io.Serializable;
import java.io.*;
import java.util.*;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.faces.event.ActionEvent;
import javax.servlet.*;
import javax.servlet.http.*;
public class AsposeContext implements Serializable
{
private String _outputType = "DOC";
public String getOutputtype() {
return _outputType;
}
public void setOutputtype(String v) {
_outputType = v;
if("DOC".equals(_outputType))
{
_mimeType = "application/msword";
}
else if("DOCX".equals(_outputType))
{
_mimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
}
else if("HTML".equals(_outputType))
{
_mimeType = "text/html";
}
else if("TXT".equals(_outputType))
{
_mimeType = "text/plain";
}
else if("ODT".equals(_outputType))
{
_mimeType = "application/vnd.oasis.opendocument.text";
}
else if("PDF".equals(_outputType))
{
_mimeType = "application/pdf";
}else
{
_mimeType = "application/msword";
}
}
private String _mimeType = "application/msword";
public String getMimetype() {
return _mimeType;
}
private Boolean _openNew = false;
public Boolean getOpennewwindow() {
return _openNew;
}
public void setOpennewwindow(Boolean v) {
_openNew = v;
}
public AsposeContext() {
}
}
|
| JAVA |
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
package com.aspose.words.demos;
import com.aspose.words.*;
import java.io.*;
import java.net.URI;
import java.net.URL;
import java.sql.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.faces.context.FacesContext;
import java.text.SimpleDateFormat;
import com.icesoft.faces.context.ByteArrayResource;
import com.icesoft.faces.context.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.lang.reflect.*;
public abstract class Demo
{
private void initDemo() throws Exception
{
mProjectDir = getProjectDir();
File baseDir = mProjectDir.getParentFile();
mDocumentsDir = new File(baseDir, "Documents");
mGeneratedDir = new File(mProjectDir, "generated");
mDatabaseDir = new File(baseDir, "Database");
mDatabase = new File(mDatabaseDir, "Northwind.mdb");
mResultSetCollection = new ArrayList();
checkExistense();
if (mGeneratedDir.exists())
{
File[] files = mGeneratedDir.listFiles();
for (int i = 0; i < files.length; i++)
files[i].delete();
}
else
{
mGeneratedDir.mkdir();
}
createConnection();
mHtmlImagesDir = new File(request.getSession().getServletContext().getRealPath("../"), "aspose.words\\Documents");
mHtmlImageAliasPath = new URI(request.getScheme(), null, request.getServerName(), SERVER_PORT, "/aspose.words/Documents/", null, null);
if(!mHtmlImagesDir.exists())
{
mHtmlImagesDir.mkdirs();
}
for(File file : mHtmlImagesDir.listFiles())
{
String ext = null;
String s = file.getName();
int i = s.lastIndexOf('.');
if (i > 0 && i < s.length() - 1) {
ext = s.substring(i+1).toLowerCase();
}
if(ext != null && ext.equals("png"))
{
Date fileDate = new Date(file.lastModified());
Date currentDate = new Date();
long diff = currentDate.getTime() - fileDate.getTime();
int hoursDiff = (int)(diff / (1000 * 60 * 60));
if(hoursDiff > 24)
file.delete();
}
}
initLicense(new com.aspose.words.License());
initLicense(new com.aspose.cells.License());
initLicense(new com.aspose.barcode.License());
isInitialized = true;
}
private static File getProjectDir()
{
URL url = Demo.class.getResource("Demo.class");
String path = new File(url.getPath()).getAbsolutePath();
String subPath = "\\classes\\com\\aspose\\words\\demos\\Demo.class";
return new File(path.substring(0, path.length() - subPath.length()));
}
private static void checkExistense()
{
if (!mProjectDir.exists())
throw new IllegalArgumentException("Can't find the Project dir: " + mProjectDir.getAbsolutePath());
if (!mDocumentsDir.exists())
throw new IllegalArgumentException("Can't find the template documents dir: " + mDocumentsDir.getAbsolutePath());
if (!mDatabaseDir.exists())
throw new IllegalArgumentException("Can't find the database dir: " + mDatabaseDir.getAbsolutePath());
if (!mDatabase.exists())
throw new IllegalArgumentException("Can't find the database file: " + mDatabase.getAbsolutePath());
}
public static Statement createStatement() throws Exception
{
return mConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
}
public static void closeResources() throws Exception
{
for(ResultSet rs : (Iterable<ResultSet>) mResultSetCollection)
{
if(rs != null)
{
Statement statement = rs.getStatement();
rs.close();
if(statement != null)
statement.close();
}
}
mConnection.close();
}
protected void init(String demoName) throws Exception
{
context = FacesContext.getCurrentInstance();
request = (HttpServletRequest) context.getExternalContext().getRequest();
response = (HttpServletResponse) context.getExternalContext().getResponse();
if(!isInitialized)
initDemo();
mDemoName = demoName;
File docTemplate = new File(mDocumentsDir, mDemoName + ".doc");
File docxTemplate = new File(mDocumentsDir, mDemoName + ".docx");
if (docTemplate.exists())
mTemplate = docTemplate;
else if (docxTemplate.exists())
mTemplate = docxTemplate;
else
throw new IllegalArgumentException("Can't find the template document file for demo: " + demoName);
}
public static void createConnection() throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String connectionString = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};" +
"DBQ=" + mDatabase + ";UID=Admin";
mConnection = DriverManager.getConnection(connectionString);
}
public abstract Document execute() throws Exception;
protected ResultSet executeQuery(String query) throws Exception
{
ResultSet rs = createStatement().executeQuery(query);
mResultSetCollection.add(rs);
return rs;
}
String getDemoName()
{
return mDemoName;
}
String getPathToGeneratedDoc()
{
return new File(mGeneratedDir, mDemoName + ".doc").getAbsolutePath();
}
String getPathToGeneratedHtml()
{
return new File(mGeneratedDir, mDemoName + ".html").getAbsolutePath();
}
String getPathToGeneratedDocx()
{
return new File(mGeneratedDir, mDemoName + ".docx").getAbsolutePath();
}
String getPathToGeneratedTxt()
{
return new File(mGeneratedDir, mDemoName + ".txt").getAbsolutePath();
}
static File getRootDistributiveDir()
{
assert mProjectDir != null;
return mProjectDir.getParentFile().getParentFile();
}
String getPathToTemplateDoc()
{
return mTemplate.getAbsolutePath();
}
protected String getImagePath(){
return mDocumentsDir + "\\Aspose.Words.gif";
}
protected static File mProjectDir;
protected static File mDocumentsDir;
protected static File mGeneratedDir;
protected static File mDatabaseDir;
protected static File mHtmlImagesDir;
protected static URI mHtmlImageAliasPath;
protected static final int SERVER_PORT = 8081;
protected static File mDatabase;
protected static Connection mConnection;
protected static boolean isInitialized = false;
protected static ArrayList mResultSetCollection;
protected String mDemoName;
protected File mTemplate;
protected void sendToBrowser(Document doc, String demoName, String formatType, boolean openNewWindow)
throws Exception
{
String fileName = demoName + "." + formatType;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int saveFormat = FileFormatUtil.extensionToSaveFormat(formatType);
if(saveFormat == SaveFormat.HTML || saveFormat == SaveFormat.MHTML || saveFormat == SaveFormat.EPUB)
{
HtmlSaveOptions htmlOptions = new HtmlSaveOptions(saveFormat);
htmlOptions.setExportHeadersFootersMode(ExportHeadersFootersMode.NONE);
htmlOptions.setExportXhtmlTransitional(true);
htmlOptions.setTableWidthOutputMode(HtmlElementSizeOutputMode.NONE);
htmlOptions.setImagesFolder(mHtmlImagesDir.getAbsolutePath());
htmlOptions.setImagesFolderAlias(mHtmlImageAliasPath.toString());
doc.save(out, htmlOptions);
}
else
{
doc.save(out, saveFormat);
}
setResponse(fileName, out);
}
protected FacesContext context;
protected HttpServletRequest request;
protected HttpServletResponse response;
protected final String FILE_NAME = "result.doc";
public Resource getGeneratedfile() {
byte[] res = getResult();
if (null==res || res.length<1) {
return null;
}
return new ByteArrayResource(res);
}
private String fileName = FILE_NAME;
public String getFilename() {
return fileName;
}
private String respType = "application/msword";
public String getResptype() {
return respType;
}
public void setResptype(String v) {
respType = v;
}
public void setFilename(String v) {
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MMM-dd_hh_mm_ss-", Locale.US);
String curDate = formatter.format(new Date());
this.fileName = curDate + v;
}
public byte[] getResult()
{
return resp;
}
protected void setResponse(String fName, ByteArrayOutputStream out) throws IOException {
setFilename(fName);
resp = out.toByteArray();
out.close();
}
protected byte[] resp;
public static void initLicense(Object obj) {
try {
Class myclass = Class.forName("com.aspose.demos.Common");
if (null==myclass) {
System.out.println("initLicense not found");
return ;
}
Method[] methods = myclass.getMethods();
Object object = myclass.newInstance();
Class partypes[] = new Class[1];
partypes[0] = Object.class;
Method meth = myclass.getMethod("initLicense", partypes);
if (null!=meth) {
Object arglist[] = new Object[1];
arglist[0] = obj;
meth.invoke(object, arglist);
}
} catch (ClassNotFoundException cnfe) {
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
|
| XHTML |
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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ice="http://www.icesoft.com/icefaces/component"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:jsf="http://www.aspose.com/jsf"
>
<ui:composition template="/WEB-INF/includes/templates/page-template.xhtml">
<ui:define name="pageTitle">
Sales Invoice - Aspose.Words Demos
</ui:define>
<ui:define name="page-content">
<ui:decorate template="/WEB-INF/includes/templates/tabbed_container.xhtml">
<ui:define name="example">
<p class="componentDescriptionTxt">Welcome to the <STRONG> Aspose.Words for Java</STRONG> Featured Demos!</p>
<p class="componentDescriptionTxt">
This demo shows one of the ways you can generate a sales invoice with a header, order details and a summary with Aspose.Words. In this demo you will
learn how to mail merge data from multiple tables, format date and numeric fields and use merge regions to grow portions of the document.
</p>
<P class="componentDescriptionTxt"><STRONG>Important</STRONG>: To produce Word documents, the machine to
run Aspose.Words for Java does not need to have Microsoft Word and Windows installed.
However, to view the contents of Word documents produced by demos, the machine to view
them needs at least Microsoft Word Viewer installed. Microsoft Word Viewer can be
<A href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=3657ce88-7cfa-457a-9aec-f4f827f20cac&displaylang=en">
downloaded</A> for free.</P>
<P class="componentDescriptionTxt">In all of these examples no OLE automation is used, all work is
performed by Aspose.Words for Java without Microsoft Word installed on the web server.</P>
<ui:include src="/WEB-INF/includes//templates/selectformat.xhtml" />
<h:commandButton id="generate" action="#{salesinv.executeDemo}" value="Generate" />
<ice:outputResource id="outResource"
mimeType="application/msword"
resource="#{salesinv.generatedfile}"
fileName="#{salesinv.filename}"
shared="false" />
</ui:define>
</ui:decorate>
</ui:define>
</ui:composition>
</html>
|
|