3
3
import com .aspose .words .Document ;
4
4
import com .aspose .words .MailMergeCleanupOptions ;
5
5
import com .aspose .words .examples .Utils ;
6
+ import com .aspose .words .net .System .Data .DataSet ;
6
7
7
8
public class MailMergeCleanUp {
8
9
public static void main (String [] args ) throws Exception {
9
10
String dataDir = Utils .getSharedDataDir (MailMergeCleanUp .class ) + "MailMerge/" ;
10
11
12
+ RemoveUnmergedRegions (dataDir );
13
+ RemoveEmptyParagraphs (dataDir );
11
14
cleanupParagraphsWithPunctuationMarks (dataDir );
12
15
}
13
16
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
+
14
91
public static void cleanupParagraphsWithPunctuationMarks (String dataDir ) throws Exception {
15
- // ExStart:CleanupParagraphsWithPunctuationMarks
16
92
// Open the document
17
93
Document doc = new Document (dataDir + "MailMerge.CleanupPunctuationMarks.docx" );
18
94
@@ -24,7 +100,6 @@ public static void cleanupParagraphsWithPunctuationMarks(String dataDir) throws
24
100
dataDir = dataDir + "MailMerge.CleanupPunctuationMarks_out.docx" ;
25
101
// Save the output document to disk.
26
102
doc .save (dataDir );
27
- // ExEnd:CleanupParagraphsWithPunctuationMarks
28
103
29
104
System .out .println ("\n Mail merge performed with cleanup paragraphs having punctuation marks successfully.\n File saved at " + dataDir );
30
105
}
0 commit comments