Skip to content

Commit f0ebed3

Browse files
committed
Added Updated Code Examples for Mail Merge Section
1 parent ce76b2e commit f0ebed3

27 files changed

+317
-118
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,54 @@
11
package com.aspose.words.examples.mail_merge;
22

33
import com.aspose.words.Document;
4+
import com.aspose.words.DocumentBuilder;
5+
import com.aspose.words.FieldNextIf;
6+
import com.aspose.words.FieldSkipIf;
7+
import com.aspose.words.FieldType;
48

59
public class AdvancedMailMergeFeatures {
610

7-
public static void main(String[] args) {
11+
public static void main(String[] args) throws Exception{
812

13+
CompareTwoExpressions();
914
}
1015

16+
private static void CompareTwoExpressions() throws Exception {
17+
Document doc = new Document();
18+
DocumentBuilder builder = new DocumentBuilder(doc);
19+
20+
//ExStart: CompareTwoExpressions
21+
// Use NextIf field
22+
FieldNextIf fieldNextIf = (FieldNextIf)builder.insertField(FieldType.FIELD_NEXT_IF, true);
23+
24+
// Or use SkipIf field
25+
FieldSkipIf fieldSkipIf = (FieldSkipIf)builder.insertField(FieldType.FIELD_SKIP_IF, true);
26+
27+
// Compare two expressions
28+
fieldNextIf.setLeftExpression("3");
29+
fieldNextIf.setRightExpression("1 + 2");
30+
fieldNextIf.setComparisonOperator("=");
31+
//ExEnd: CompareTwoExpressions
32+
}
1133
/**
1234
* Add a mapping when a merge field in a document and a data field in a data
1335
* source have different names.
1436
*/
1537
private static void addMappingWhenMergeFieldAndDataFieldHaveDifferentNames(Document doc) {
16-
//ExStart:addMappingWhenMergeFieldAndDataFieldHaveDifferentNames
1738
doc.getMailMerge().getMappedDataFields().add("MyFieldName_InDocument", "MyFieldName_InDataSource");
18-
//ExEnd:addMappingWhenMergeFieldAndDataFieldHaveDifferentNames
1939
}
2040

2141
/**
2242
* Get names of all merge fields in a document.
2343
*/
2444
private static void getNamesOfAllMergeFields(Document doc) throws Exception {
25-
//ExStart:getNamesOfAllMergeFields
2645
String[] fieldNames = doc.getMailMerge().getFieldNames();
27-
//ExEnd:getNamesOfAllMergeFields
2846
}
2947

3048
/**
3149
* Delete all merge fields from a document without executing mail merge.
3250
*/
3351
private static void deletingMergeFields(Document doc) throws Exception {
34-
//ExStart:deletingMergeFields
3552
doc.getMailMerge().deleteFields();
36-
//ExEnd:deletingMergeFields
3753
}
3854
}

Examples/src/main/java/com/aspose/words/examples/mail_merge/ExecuteMailMergeWithRegions.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
import com.aspose.words.Document;
44
import com.aspose.words.examples.Utils;
55
import com.aspose.words.net.System.Data.DataTable;
6+
import com.aspose.words.MailMergeRegionInfo;
7+
import com.sun.tools.javac.util.Assert;
8+
import java.util.*;
69

710
public class ExecuteMailMergeWithRegions {
8-
//ExStart:
11+
912
private static final String dataDir = Utils.getSharedDataDir(ExecuteMailMergeWithRegions.class) + "MailMerge/";
1013

1114
public static void main(String[] args) throws Exception {
15+
16+
GetRegionsByName();
17+
1218
Document doc = new Document(dataDir + "MailMerge.ExecuteWithRegions.doc");
1319

1420
int orderId = 10444;
@@ -68,5 +74,23 @@ private static java.sql.ResultSet executeDataTable(String commandText) throws Ex
6874
java.sql.Statement statement = conn.createStatement();
6975
return statement.executeQuery(commandText);
7076
}
71-
//ExEnd:
77+
78+
private static void GetRegionsByName() throws Exception {
79+
//ExStart: GetRegionsByName
80+
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
81+
Document doc = new Document(dataDir + "Mail merge regions.docx");
82+
83+
List<MailMergeRegionInfo> regions = doc.getMailMerge().getRegionsByName("Region1");
84+
for (MailMergeRegionInfo region : regions)
85+
System.out.println(region.getName());
86+
87+
regions = doc.getMailMerge().getRegionsByName("Region2");
88+
for (MailMergeRegionInfo region : regions)
89+
System.out.println(region.getName());
90+
91+
regions = doc.getMailMerge().getRegionsByName("NestedRegion1");
92+
for (MailMergeRegionInfo region : regions)
93+
System.out.println(region.getName());
94+
//ExEnd: GetRegionsByName
95+
}
7296
}

Examples/src/main/java/com/aspose/words/examples/mail_merge/MailMergeCleanUp.java

+77-2
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,92 @@
33
import com.aspose.words.Document;
44
import com.aspose.words.MailMergeCleanupOptions;
55
import com.aspose.words.examples.Utils;
6+
import com.aspose.words.net.System.Data.DataSet;
67

78
public class MailMergeCleanUp {
89
public static void main(String[] args) throws Exception {
910
String dataDir = Utils.getSharedDataDir(MailMergeCleanUp.class) + "MailMerge/";
1011

12+
RemoveUnmergedRegions(dataDir);
13+
RemoveEmptyParagraphs(dataDir);
1114
cleanupParagraphsWithPunctuationMarks(dataDir);
1215
}
1316

17+
public static void RemoveEmptyTableRows(String dataDir) throws Exception {
18+
//ExStart: RemoveEmptyTableRows
19+
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
20+
Document doc = new Document(dataDir + "RemoveRowfromTable.docx");
21+
22+
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_EMPTY_TABLE_ROWS);
23+
24+
doc.getMailMerge().execute(new String[] { "FullName", "Company", "Address", "Address2", "City" },
25+
new Object[] { "James Bond", "MI5 Headquarters", "Milbank", "", "London" });
26+
27+
doc.save(dataDir + "MailMerge.ExecuteArray_out.doc");
28+
//ExEnd: RemoveEmptyTableRows
29+
}
30+
public static void RemoveContainingFields(String dataDir) throws Exception {
31+
//ExStart: RemoveContainingFields
32+
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
33+
Document doc = new Document(dataDir + "RemoveRowfromTable.docx");
34+
35+
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS);
36+
37+
doc.getMailMerge().execute(new String[] { "FullName", "Company", "Address", "Address2", "City" },
38+
new Object[] { "James Bond", "MI5 Headquarters", "Milbank", "", "London" });
39+
40+
doc.save(dataDir + "MailMerge.ExecuteArray_out.doc");
41+
//ExEnd: RemoveContainingFields
42+
}
43+
public static void RemoveUnusedFields(String dataDir) throws Exception {
44+
//ExStart:RemoveUnusedFields
45+
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
46+
Document doc = new Document(dataDir + "RemoveRowfromTable.docx");
47+
48+
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS);
49+
50+
doc.getMailMerge().execute(new String[] { "FullName", "Company", "Address", "Address2", "City" },
51+
new Object[] { "James Bond", "MI5 Headquarters", "Milbank", "", "London" });
52+
53+
doc.save(dataDir + "MailMerge.ExecuteArray_out.doc");
54+
//ExEnd:RemoveUnusedFields
55+
}
56+
57+
public static void RemoveUnmergedRegions(String dataDir) throws Exception {
58+
//ExStart:RemoveUnmergedRegions
59+
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
60+
Document doc = new Document(dataDir + "TestFile Empty.doc");
61+
62+
// Create an empty data source in the form of a DataSet containing no DataTable objects.
63+
DataSet data = new DataSet();
64+
65+
// Enable the MailMergeCleanupOptions.RemoveUnusedRegions option.
66+
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS);
67+
68+
// Merge the data with the document by executing mail merge which will have no effect as there is no data.
69+
// However the regions found in the document will be removed automatically as they are unused.
70+
doc.getMailMerge().executeWithRegions(data);
71+
72+
// Save the output document to disk.
73+
doc.save(dataDir + "TestFile.RemoveEmptyRegions Out.doc");
74+
//ExEnd:RemoveUnmergedRegions
75+
}
76+
77+
public static void RemoveEmptyParagraphs(String dataDir) throws Exception {
78+
//ExStart: RemoveEmptyParagraphs
79+
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
80+
Document doc = new Document(dataDir + "RemoveRowfromTable.docx");
81+
82+
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS);
83+
84+
doc.getMailMerge().execute(new String[] { "FullName", "Company", "Address", "Address2", "City" },
85+
new Object[] { "James Bond", "MI5 Headquarters", "Milbank", "", "London" });
86+
87+
doc.save(dataDir + "MailMerge.ExecuteArray_out.doc");
88+
//ExEnd: RemoveEmptyParagraphs
89+
}
90+
1491
public static void cleanupParagraphsWithPunctuationMarks(String dataDir) throws Exception {
15-
// ExStart:CleanupParagraphsWithPunctuationMarks
1692
// Open the document
1793
Document doc = new Document(dataDir + "MailMerge.CleanupPunctuationMarks.docx");
1894

@@ -24,7 +100,6 @@ public static void cleanupParagraphsWithPunctuationMarks(String dataDir) throws
24100
dataDir = dataDir + "MailMerge.CleanupPunctuationMarks_out.docx";
25101
// Save the output document to disk.
26102
doc.save(dataDir);
27-
// ExEnd:CleanupParagraphsWithPunctuationMarks
28103

29104
System.out.println("\nMail merge performed with cleanup paragraphs having punctuation marks successfully.\nFile saved at " + dataDir);
30105
}

Examples/src/main/java/com/aspose/words/examples/mail_merge/MailMergeUsingMustacheTemplateSyntax.java

+38-15
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,47 @@
22

33
import com.aspose.words.Document;
44
import com.aspose.words.examples.Utils;
5+
import com.aspose.words.net.System.Data.DataRow;
6+
import com.aspose.words.net.System.Data.DataTable;
57

68
import javax.xml.parsers.DocumentBuilderFactory;
79

810
public class MailMergeUsingMustacheTemplateSyntax {
911

10-
private static final String dataDir = Utils.getSharedDataDir(MailMergeUsingMustacheTemplateSyntax.class) + "MailMerge/";
11-
1212
public static void main(String[] args) throws Exception {
13+
String dataDir = Utils.getSharedDataDir(MailMergeUsingMustacheTemplateSyntax.class) + "MailMerge/";
14+
1315
// Performs a simple insertion of data into merge fields and sends the document to the browser inline.
14-
simpleInsertionOfDataIntoMergeFields();
15-
16-
useMailMergeUsingMustacheSyntax();
17-
useOfifelseMustacheSyntax();
16+
simpleInsertionOfDataIntoMergeFields(dataDir);
17+
MustacheSyntaxUsingDataTable(dataDir);
18+
useMailMergeUsingMustacheSyntax(dataDir);
19+
UseOfIfElseMustacheSyntax(dataDir);
1820
}
1921

20-
public static void simpleInsertionOfDataIntoMergeFields() throws Exception {
21-
//ExStart:simpleInsertionOfDataIntoMergeFields
22+
private static void MustacheSyntaxUsingDataTable(String dataDir) throws Exception {
23+
//ExStart: MustacheSyntaxUsingDataTable
24+
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
25+
// Load a document
26+
Document doc = new Document(dataDir + "Test.docx");
27+
28+
// Loop through each row and fill it with data
29+
DataTable dataTable = new DataTable("list");
30+
dataTable.getColumns().add("Number");
31+
for (int i = 0; i < 10; i++)
32+
{
33+
DataRow datarow = dataTable.newRow();
34+
dataTable.getRows().add(datarow);
35+
datarow.set("Number " + i, i);
36+
}
37+
38+
// Activate performing a mail merge operation into additional field types
39+
doc.getMailMerge().setUseNonMergeFields(true);
40+
doc.getMailMerge().executeWithRegions(dataTable);
41+
doc.save(dataDir + "MailMerge.Mustache.docx");
42+
//ExEnd:MustacheSyntaxUsingDataTable
43+
}
44+
45+
public static void simpleInsertionOfDataIntoMergeFields(String dataDir) throws Exception {
2246
// Open an existing document.
2347
Document doc = new Document(dataDir + "MailMerge.ExecuteArray.doc");
2448

@@ -28,11 +52,9 @@ public static void simpleInsertionOfDataIntoMergeFields() throws Exception {
2852
doc.getMailMerge().execute(new String[]{"FullName", "Company", "Address", "Address2", "City"}, new Object[]{"James Bond", "MI5 Headquarters", "Milbank", "", "London"});
2953

3054
doc.save(dataDir + "MailMerge.ExecuteArray_Out.doc");
31-
//ExEnd:simpleInsertionOfDataIntoMergeFields
3255
}
3356

34-
public static void useMailMergeUsingMustacheSyntax() throws Exception {
35-
//ExStart:useMailMergeUsingMustacheSyntax
57+
public static void useMailMergeUsingMustacheSyntax(String dataDir) throws Exception {
3658
// Use DocumentBuilder from the javax.xml.parsers package and Document class from the org.w3c.dom package to read
3759
// the XML data file and store it in memory.
3860
javax.xml.parsers.DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
@@ -51,20 +73,21 @@ public static void useMailMergeUsingMustacheSyntax() throws Exception {
5173

5274
// Save the output document.
5375
doc.save(dataDir + "MailMergeUsingMustacheSyntax_Out.docx");
54-
//ExEnd:useMailMergeUsingMustacheSyntax
5576
}
5677

57-
public static void useOfifelseMustacheSyntax() throws Exception {
58-
// ExStart:UseOfifelseMustacheSyntax
78+
public static void UseOfIfElseMustacheSyntax(String dataDir) throws Exception {
79+
// ExStart:UseOfIfElseMustacheSyntax
80+
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
5981
// Open a template document.
6082
Document doc = new Document(dataDir + "UseOfifelseMustacheSyntax.docx");
6183

6284
doc.getMailMerge().setUseNonMergeFields(true);
85+
6386
doc.getMailMerge().execute(new String[]{"GENDER"}, new Object[]{"MALE"});
6487

6588
// Save the output document.
6689
doc.save(dataDir + "MailMergeUsingMustacheSyntaxifelse_out.docx");
67-
// ExEnd:UseOfifelseMustacheSyntax
90+
// ExEnd:UseOfIfElseMustacheSyntax
6891
System.out.println("\nMail merge performed with mustache if else syntax successfully.\nFile saved at " + dataDir);
6992
}
7093
}

Examples/src/main/java/com/aspose/words/examples/mail_merge/RemoveEmptyRegions.java

-36
This file was deleted.

Examples/src/main/java/com/aspose/words/examples/mail_merge/RemoveUnmergedRegions.java

-31
This file was deleted.

0 commit comments

Comments
 (0)