public class MailMerge
extends java.lang.Object
To learn more, visit the Mail Merge and Reporting documentation article.
For mail merge operation to work, the document should contain Word MERGEFIELD and optionally NEXT fields. During mail merge operation, merge fields in the document are replaced with values from your data source.
There are two distinct ways to use mail merge: with mail merge regions and without.
The simplest mail merge is without regions and it is very similar to how mail merge works in Word. Use Execute methods to merge information from some data source such as DataTable, DataSet or an array of objects into your document. The MailMerge object processes all records of the data source and copies and appends content of the whole document for each record.
Note that when MailMerge object encounters a NEXT field, it selects next record in the data source and continues merging without copying any content.
Use ExecuteWithRegions methods to merge information into a document with mail merge regions defined. You can use or as data sources for this operation.
You need to use mail merge regions if you want to dynamically grow portions inside the document. Without mail merge regions whole document will be repeated for every record of the data source.
Document,
Document.getMailMerge()| Modifier and Type | Method and Description |
|---|---|
void |
deleteFields()
Removes mail merge related fields from the document.
|
void |
execute(DataRow row)
Performs mail merge from a DataRow into the document.
|
void |
execute(DataTable table)
Performs mail merge from a com.aspose.words.net.System.Data.DataTable into the document.
|
void |
execute(DataView dataView)
Performs mail merge from a DataView into the document.
|
void |
execute(IDataReader dataReader)
Performs mail merge from IDataReader into the document.
|
void |
execute(IMailMergeDataSource dataSource)
Performs a mail merge from a custom data source.
|
void |
execute(java.lang.String[] fieldNames,
java.lang.Object[] values)
|
void |
executeWithRegions(DataSet dataSet)
|
void |
executeWithRegions(DataTable dataTable)
Performs mail merge from a DataTable into the document with mail merge regions.
|
void |
executeWithRegions(DataView dataView)
Performs mail merge from a DataView into the document with mail merge regions.
|
void |
executeWithRegions(IDataReader dataReader,
java.lang.String tableName)
Performs mail merge from IDataReader into the document with mail merge regions.
|
void |
executeWithRegions(IMailMergeDataSource dataSource)
Performs a mail merge from a custom data source with mail merge regions.
|
void |
executeWithRegions(IMailMergeDataSourceRoot dataSourceRoot)
Performs a mail merge from a custom data source with mail merge regions.
|
int |
getCleanupOptions()
Gets a set of flags that specify what items should be removed during mail merge.
|
boolean |
getCleanupParagraphsWithPunctuationMarks()
Gets a value indicating whether paragraphs with punctuation marks are considered as empty and should be removed if the
MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS option is specified. |
IFieldMergingCallback |
getFieldMergingCallback()
Occurs during mail merge when a mail merge field is encountered in the document.
|
java.lang.String[] |
getFieldNames()
Returns a collection of mail merge field names available in the document.
|
java.lang.String[] |
getFieldNamesForRegion(java.lang.String regionName)
|
java.lang.String[] |
getFieldNamesForRegion(java.lang.String regionName,
int regionIndex)
Returns a collection of mail merge field names available in the region.
|
IMailMergeCallback |
getMailMergeCallback()
Allows to handle particular events during mail merge.
|
MappedDataFieldCollection |
getMappedDataFields()
Returns a collection that represents mapped data fields for the mail merge operation.
|
boolean |
getMergeDuplicateRegions()
Gets a value indicating whether all of the document mail merge regions with the name of a data source should be merged while executing of a mail merge with regions against the data source or just the first one.
|
boolean |
getMergeWholeDocument()
Gets a value indicating whether fields in whole document are updated while executing of a mail merge with regions.
|
boolean |
getPreserveUnusedTags()
Gets a value indicating whether the unused "mustache" tags should be preserved.
|
java.lang.String |
getRegionEndTag()
Gets a mail merge region end tag.
|
java.util.ArrayList |
getRegionsByName(java.lang.String regionName)
Returns a collection of mail merge regions with the specified name.
|
MailMergeRegionInfo |
getRegionsHierarchy()
Returns a full hierarchy of regions (with fields) available in the document.
|
java.lang.String |
getRegionStartTag()
Gets a mail merge region start tag.
|
boolean |
getRestartListsAtEachSection()
Gets a value indicating whether lists are restarted at each section after executing of a mail merge.
|
boolean |
getRetainFirstSectionStart()
Gets a value indicating whether the
PageSetup.getSectionStart() / PageSetup.setSectionStart(int) of the first document section and its copies for subsequent data source rows are retained during mail merge or updated according to MS Word behaviour. |
boolean |
getTrimWhitespaces()
Gets a value indicating whether trailing and leading whitespaces are trimmed from mail merge values.
|
boolean |
getUnconditionalMergeFieldsAndRegions()
Gets a value indicating whether merge fields and merge regions are merged regardless of the parent IF field's condition.
|
boolean |
getUseNonMergeFields()
When true, specifies that in addition to MERGEFIELD fields, mail merge is performed into some other types of fields and also into "{{fieldName}}" tags.
|
boolean |
getUseWholeParagraphAsRegion()
Gets a value indicating whether whole paragraph with TableStart or TableEnd field or particular range between TableStart and TableEnd fields should be included into mail merge region.
|
void |
setCleanupOptions(int value)
Sets a set of flags that specify what items should be removed during mail merge.
|
void |
setCleanupParagraphsWithPunctuationMarks(boolean value)
Sets a value indicating whether paragraphs with punctuation marks are considered as empty and should be removed if the
MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS option is specified. |
void |
setFieldMergingCallback(IFieldMergingCallback value)
Occurs during mail merge when a mail merge field is encountered in the document.
|
void |
setMailMergeCallback(IMailMergeCallback value)
Allows to handle particular events during mail merge.
|
void |
setMergeDuplicateRegions(boolean value)
Sets a value indicating whether all of the document mail merge regions with the name of a data source should be merged while executing of a mail merge with regions against the data source or just the first one.
|
void |
setMergeWholeDocument(boolean value)
Sets a value indicating whether fields in whole document are updated while executing of a mail merge with regions.
|
void |
setPreserveUnusedTags(boolean value)
Sets a value indicating whether the unused "mustache" tags should be preserved.
|
void |
setRegionEndTag(java.lang.String value)
Sets a mail merge region end tag.
|
void |
setRegionStartTag(java.lang.String value)
Sets a mail merge region start tag.
|
void |
setRestartListsAtEachSection(boolean value)
Sets a value indicating whether lists are restarted at each section after executing of a mail merge.
|
void |
setRetainFirstSectionStart(boolean value)
Sets a value indicating whether the
PageSetup.getSectionStart() / PageSetup.setSectionStart(int) of the first document section and its copies for subsequent data source rows are retained during mail merge or updated according to MS Word behaviour. |
void |
setTrimWhitespaces(boolean value)
Sets a value indicating whether trailing and leading whitespaces are trimmed from mail merge values.
|
void |
setUnconditionalMergeFieldsAndRegions(boolean value)
Sets a value indicating whether merge fields and merge regions are merged regardless of the parent IF field's condition.
|
void |
setUseNonMergeFields(boolean value)
When true, specifies that in addition to MERGEFIELD fields, mail merge is performed into some other types of fields and also into "{{fieldName}}" tags.
|
void |
setUseWholeParagraphAsRegion(boolean value)
Sets a value indicating whether whole paragraph with TableStart or TableEnd field or particular range between TableStart and TableEnd fields should be included into mail merge region.
|
public void execute(IMailMergeDataSource dataSource) throws java.lang.Exception
Use this method to fill mail merge fields in the document with values from any data source such as a list or hashtable or objects. You need to write your own class that implements the IMailMergeDataSource interface.
You can use this method only when FieldOptions.isBidiTextSupportedOnUpdate() / FieldOptions.isBidiTextSupportedOnUpdate(boolean) is false, that is you do not need Right-To-Left language (such as Arabic or Hebrew) compatibility.
This method ignores the MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS option.
dataSource - An object that implements the custom mail merge data source interface.java.lang.Exceptionpublic void execute(java.lang.String[] fieldNames,
java.lang.Object[] values)
throws java.lang.Exception
Use this method to fill mail merge fields in the document with values from an array of objects.
This method merges data for one record only. The array of field names and the array of values represent the data of a single record.
This method does not use mail merge regions.
This method ignores the MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS option.
fieldNames - Array of merge field names. Field names are not case sensitive. If a field name that is not found in the document is encountered, it is ignored.values - Array of values to be inserted into the merge fields. Number of elements in this array must be the same as the number of elements in fieldNames.java.lang.Exceptionpublic void execute(DataTable table) throws java.lang.Exception
Use this method to fill mail merge fields in the document with values from a DataTable.
All records from the table are merged into the document.
You can use NEXT field in the Word document to cause MailMerge object to select next record from the DataTable and continue merging. This can be used when creating documents such as mailing labels.
When MailMerge object reaches end of the main document and there are still more rows in the DataTable, it copies entire content of the main document and appends it to the end of the destination document using a section break as a separator.
This method ignores the MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS option.
table - Table that contains data to be inserted into mail merge fields. Field names are not case sensitive. If a field name that is not found in the document is encountered, it is ignored.java.lang.Exceptionpublic void execute(IDataReader dataReader) throws java.lang.Exception
You can pass SqlDataReader or OleDbDataReader object into this method as a parameter because they both implemented IDataReader interface.
Note this method does not use mail merge regions and for multiple records the document will grow by repeating the whole document.
This method ignores the MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS option.
dataReader - Data source for the mail merge operation.java.lang.Exceptionpublic void execute(DataView dataView) throws java.lang.Exception
This method is useful if you retrieve data into a DataTable but then need to apply a filter or sort before the mail merge.
Note this method does not use mail merge regions and for multiple records the document will grow by repeating the whole document.
This method ignores the MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS option.
dataView - Data source for the mail merge operation.java.lang.Exceptionpublic void execute(DataRow row) throws java.lang.Exception
Use this method to fill mail merge fields in the document with values from a DataRow.
This method ignores the MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS option.
row - Row that contains data to be inserted into mail merge fields. Field names are not case sensitive. If a field name that is not found in the document is encountered, it is ignored.java.lang.Exceptionpublic void executeWithRegions(IMailMergeDataSource dataSource) throws java.lang.Exception
Use this method to fill mail merge fields in the document with values from any custom data source such as an XML file or collections of business objects. You need to write your own class that implements the IMailMergeDataSource interface.
You can use this method only when FieldOptions.isBidiTextSupportedOnUpdate() / FieldOptions.isBidiTextSupportedOnUpdate(boolean) is false, that is you do not need Right-To-Left language (such as Arabic or Hebrew) compatibility.
dataSource - An object that implements the custom mail merge data source interface.java.lang.Exceptionpublic void executeWithRegions(IMailMergeDataSourceRoot dataSourceRoot) throws java.lang.Exception
Use this method to fill mail merge fields in the document with values from any custom data source such as an XML file or collections of business objects. You need to write your own classes that implement the IMailMergeDataSourceRoot and IMailMergeDataSource interfaces.
You can use this method only when FieldOptions.isBidiTextSupportedOnUpdate() / FieldOptions.isBidiTextSupportedOnUpdate(boolean) is false, that is you do not need Right-To-Left language (such as Arabic or Hebrew) compatibility.
dataSourceRoot - An object that implements the custom mail merge data source root interface.java.lang.Exceptionpublic void executeWithRegions(DataSet dataSet) throws java.lang.Exception
Use this method to perform mail merge from one or more tables into repeatable mail merge regions in the document. The mail merge regions inside the document will dynamically grow to accommodate records in the corresponding tables.
The document must have mail merge regions defined with names that refer to the tables in the DataSet.
To specify a mail merge region in the document you need to insert two mail merge fields to mark beginning and end of the mail merge region.
All document content that is included inside a mail merge region will be automatically repeated for every record in the DataTable.
To mark beginning of a mail merge region insert a MERGEFIELD with name TableStart:MyTable, where MyTable corresponds to one of the table names in your DataSet.
To mark the end of the mail merge region insert another MERGEFIELD with name TableEnd:MyTable.
To insert a MERGEFIELD in Word use Insert/Field command and select MergeField then type the name of the field.
The TableStart and TableEnd fields must be inside the same section in your document.
If used inside a table, TableStart and TableEnd must be inside the same row in the table.
Mail merge regions in a document should be well formed (there always needs to be a pair of matching TableStart and TableEnd merge fields with the same table name).
dataSet - DataSet that contains data to be inserted into mail merge fields.java.lang.Exceptionpublic void executeWithRegions(DataTable dataTable) throws java.lang.Exception
If there are other mail merge regions defined in the document they are left intact. This allows to perform several mail merge operations.
dataTable - Data source for the mail merge operation. The table must have its TableName property set.java.lang.Exceptionpublic void executeWithRegions(DataView dataView) throws java.lang.Exception
This method is useful if you retrieve data into a DataTable but then need to apply a filter or sort before the mail merge.
The document must have a mail merge region defined with name that matches DataView.Table.TableName.
If there are other mail merge regions defined in the document they are left intact. This allows to perform several mail merge operations.
dataView - Data source for the mail merge operation. The source table of the DataView must have its TableName property set.java.lang.Exceptionpublic void executeWithRegions(IDataReader dataReader, java.lang.String tableName) throws java.lang.Exception
You can pass SqlDataReader or OleDbDataReader object into this method as a parameter because they both implemented IDataReader interface.
dataReader - Source of the data records for mail merge such as OleDbDataReader or SqlDataReader.tableName - Name of the mail merge region in the document to populate.java.lang.Exceptionpublic java.lang.String[] getFieldNames()
throws java.lang.Exception
Returns full merge field names including optional prefix. Does not eliminate duplicate field names.
A new string[] array is created on every call.
Includes "mustache" field names if getUseNonMergeFields() / setUseNonMergeFields(boolean) is true.
java.lang.Exceptionpublic java.lang.String[] getFieldNamesForRegion(java.lang.String regionName)
throws java.lang.Exception
Returns full merge field names including optional prefix. Does not eliminate duplicate field names.
If document contains multiple regions with the same name the very first region is processed.
A new string array is created on every call.
regionName - Region name (case-insensitive).java.lang.Exceptionpublic java.lang.String[] getFieldNamesForRegion(java.lang.String regionName,
int regionIndex)
throws java.lang.Exception
Returns full merge field names including optional prefix. Does not eliminate duplicate field names.
If document contains multiple regions with the same name the Nth region (zero-based) is processed.
A new string array is created on every call.
regionName - Region name (case-insensitive).regionIndex - Region index (zero-based).java.lang.Exceptionpublic java.util.ArrayList getRegionsByName(java.lang.String regionName)
regionName - Region name (case-insensitive).public MailMergeRegionInfo getRegionsHierarchy()
Hierarchy is returned in the form of the MailMergeRegionInfo class.
public void deleteFields()
throws java.lang.Exception
This method removes MERGEFIELD and NEXT fields from the document.
This method could be useful if your mail merge operation does not always need to populate all fields in the document. Use this method to remove all remaining mail merge fields.
java.lang.Exceptionpublic java.lang.String getRegionStartTag()
public void setRegionStartTag(java.lang.String value)
value - A mail merge region start tag.public java.lang.String getRegionEndTag()
public void setRegionEndTag(java.lang.String value)
value - A mail merge region end tag.public int getCleanupOptions()
MailMergeCleanupOptions constants.public void setCleanupOptions(int value)
value - A set of flags that specify what items should be removed during mail merge. The value must be a bitwise combination of MailMergeCleanupOptions constants.public boolean getCleanupParagraphsWithPunctuationMarks()
MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS option is specified.
The default value is true.
Here is the complete list of cleanable punctuation marks:
MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS option is specified.public void setCleanupParagraphsWithPunctuationMarks(boolean value)
MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS option is specified.
The default value is true.
Here is the complete list of cleanable punctuation marks:
value - A value indicating whether paragraphs with punctuation marks are considered as empty and should be removed if the MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS option is specified.public boolean getUseNonMergeFields()
Normally, mail merge is only performed into MERGEFIELD fields, but several customers had their reporting built using other fields and had many documents created this way. To simplify migration (and because this approach was independently used by several customers) the ability to mail merge into other fields was introduced.
When UseNonMergeFields is set to true, Aspose.Words will perform mail merge into the following fields:
MERGEFIELD FieldName
MACROBUTTON NOMACRO FieldName
IF 0 = 0 "{FieldName}" ""
Also, when UserNonMergeFields is set to true, Aspose.Words will perform mail merge into text tags "{{fieldName}}". These are not fields, but just text tags.
boolean value.public void setUseNonMergeFields(boolean value)
Normally, mail merge is only performed into MERGEFIELD fields, but several customers had their reporting built using other fields and had many documents created this way. To simplify migration (and because this approach was independently used by several customers) the ability to mail merge into other fields was introduced.
When UseNonMergeFields is set to true, Aspose.Words will perform mail merge into the following fields:
MERGEFIELD FieldName
MACROBUTTON NOMACRO FieldName
IF 0 = 0 "{FieldName}" ""
Also, when UserNonMergeFields is set to true, Aspose.Words will perform mail merge into text tags "{{fieldName}}". These are not fields, but just text tags.
value - The corresponding boolean value.public boolean getPreserveUnusedTags()
getUseNonMergeFields(),
setUseNonMergeFields(boolean)public void setPreserveUnusedTags(boolean value)
value - A value indicating whether the unused "mustache" tags should be preserved.getUseNonMergeFields(),
setUseNonMergeFields(boolean)public boolean getMergeDuplicateRegions()
public void setMergeDuplicateRegions(boolean value)
value - A value indicating whether all of the document mail merge regions with the name of a data source should be merged while executing of a mail merge with regions against the data source or just the first one.public boolean getMergeWholeDocument()
public void setMergeWholeDocument(boolean value)
value - A value indicating whether fields in whole document are updated while executing of a mail merge with regions.public boolean getUseWholeParagraphAsRegion()
public void setUseWholeParagraphAsRegion(boolean value)
value - A value indicating whether whole paragraph with TableStart or TableEnd field or particular range between TableStart and TableEnd fields should be included into mail merge region.public boolean getRestartListsAtEachSection()
public void setRestartListsAtEachSection(boolean value)
value - A value indicating whether lists are restarted at each section after executing of a mail merge.public MappedDataFieldCollection getMappedDataFields()
Mapped data fields allow to automatically map between names of fields in your data source and names of mail merge fields in the document.
public IFieldMergingCallback getFieldMergingCallback()
IFieldMergingCallback value.public void setFieldMergingCallback(IFieldMergingCallback value)
value - The corresponding IFieldMergingCallback value.public IMailMergeCallback getMailMergeCallback()
IMailMergeCallback value.public void setMailMergeCallback(IMailMergeCallback value)
value - The corresponding IMailMergeCallback value.public boolean getTrimWhitespaces()
public void setTrimWhitespaces(boolean value)
value - A value indicating whether trailing and leading whitespaces are trimmed from mail merge values.public boolean getUnconditionalMergeFieldsAndRegions()
public void setUnconditionalMergeFieldsAndRegions(boolean value)
value - A value indicating whether merge fields and merge regions are merged regardless of the parent IF field's condition.public boolean getRetainFirstSectionStart()
PageSetup.getSectionStart() / PageSetup.setSectionStart(int) of the first document section and its copies for subsequent data source rows are retained during mail merge or updated according to MS Word behaviour.
The default value is true.PageSetup.getSectionStart() / PageSetup.setSectionStart(int) of the first document section and its copies for subsequent data source rows are retained during mail merge or updated according to MS Word behaviour.public void setRetainFirstSectionStart(boolean value)
PageSetup.getSectionStart() / PageSetup.setSectionStart(int) of the first document section and its copies for subsequent data source rows are retained during mail merge or updated according to MS Word behaviour.
The default value is true.value - A value indicating whether the PageSetup.getSectionStart() / PageSetup.setSectionStart(int) of the first document section and its copies for subsequent data source rows are retained during mail merge or updated according to MS Word behaviour.