com.aspose.words

Class MappedDataFieldCollection

  • java.lang.Object
    • com.aspose.words.MappedDataFieldCollection
  • All Implemented Interfaces:
    java.lang.Iterable
    public class MappedDataFieldCollection 
    extends java.lang.Object

Allows to automatically map between names of fields in your data source and names of mail merge fields in the document.

This is implemented as a collection of string keys into string values. The keys are the names of mail merge fields in the document and the values are the names of fields in your data source.

Example:

Shows how to map data columns and MERGEFIELDs with different names so the data is transferred between them during a mail merge.
public void mappedDataFieldCollection() throws Exception
{
    // Create a document and table that we will merge
    Document doc = createSourceDocMappedDataFields();
    DataTable dataTable = createSourceTableMappedDataFields();

    // We have a column "Column2" in the data table that doesn't have a respective MERGEFIELD in the document
    // Also, we have a MERGEFIELD named "Column3" that does not exist as a column in the data source
    // If data from "Column2" is suitable for the "Column3" MERGEFIELD,
    // we can map that column name to the MERGEFIELD in the "MappedDataFields" key/value pair
    MappedDataFieldCollection mappedDataFields = doc.getMailMerge().getMappedDataFields();

    // A data source column name is linked to a MERGEFIELD name by adding an element like this
    mappedDataFields.add("MergeFieldName", "DataSourceColumnName");

    // So, values from "Column2" will now go into MERGEFIELDs named "Column3" as well as "Column2", if there are any
    mappedDataFields.add("Column3", "Column2");

    // The MERGEFIELD name is the "key" to the respective data source column name "value"
    Assert.assertEquals(mappedDataFields.get("MergeFieldName"), "DataSourceColumnName");
    Assert.assertTrue(mappedDataFields.containsKey("MergeFieldName"));
    Assert.assertTrue(mappedDataFields.containsValue("DataSourceColumnName"));

    // Now if we run this mail merge, the "Column3" MERGEFIELDs will take data from "Column2" of the table
    doc.getMailMerge().execute(dataTable);

    // We can count and iterate over the mapped columns/fields
    Assert.assertEquals(mappedDataFields.getCount(), 2);

    Iterator<Map.Entry<String, String>> enumerator = mappedDataFields.iterator();
    try
    {
        while (enumerator.hasNext())
        {
            Map.Entry<String, String> dataField = enumerator.next();
            System.out.println(MessageFormat.format("Column named {0} is mapped to MERGEFIELDs named {1}", dataField.getValue(), dataField.getKey()));
        }
    }
    finally { if (enumerator != null) enumerator.remove(); }

    // We can also remove some or all of the elements
    mappedDataFields.remove("MergeFieldName");
    Assert.assertFalse(mappedDataFields.containsKey("MergeFieldName"));
    Assert.assertFalse(mappedDataFields.containsValue("DataSourceColumnName"));

    mappedDataFields.clear();
    Assert.assertEquals(mappedDataFields.getCount(), 0);

    // Removing the mapped key/value pairs has no effect on the document because the merge was already done with them in place
    doc.save(getArtifactsDir() + "MailMerge.MappedDataFieldCollection.docx");
}

/// <summary>
/// Create a document with 2 MERGEFIELDs, one of which does not have a corresponding column in the data table.
/// </summary>
private static Document createSourceDocMappedDataFields() throws Exception
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Insert two MERGEFIELDs that will accept data from that table
    builder.insertField(" MERGEFIELD Column1");
    builder.write(", ");
    builder.insertField(" MERGEFIELD Column3");

    return doc;
}

/// <summary>
/// Create a data table with 2 columns, one of which does not have a corresponding MERGEFIELD in our source document.
/// </summary>
private static DataTable createSourceTableMappedDataFields()
{
    // Create a data table that will be used in a mail merge
    DataTable dataTable = new DataTable("MyTable");
    dataTable.getColumns().add("Column1");
    dataTable.getColumns().add("Column2");
    dataTable.getRows().add(new Object[] { "Value1", "Value2" });

    return dataTable;
}
See Also:
MailMerge, MailMerge.MappedDataFields

Property Getters/Setters Summary
intgetCount()
Gets the number of elements contained in the collection.
java.lang.Stringget(java.lang.String documentFieldName)
void
set(java.lang.StringdocumentFieldName, java.lang.Stringvalue)
           Gets or sets the name of the field in the data source associated with the specified mail merge field.
 
Method Summary
voidadd(java.lang.String documentFieldName, java.lang.String dataSourceFieldName)
Adds a new field mapping.
voidclear()
Removes all elements from the collection.
booleancontainsKey(java.lang.String documentFieldName)
Determines whether a mapping from the specified field in the document exists in the collection.
booleancontainsValue(java.lang.String dataSourceFieldName)
Determines whether a mapping from the specified field in the data source exists in the collection.
java.util.Iterator<java.util.Map.Entry<java.lang.String, java.lang.String>>iterator()
Returns a dictionary iterator object that can be used to iterate over all items in the collection.
voidremove(java.lang.String documentFieldName)
Removes a field mapping.
 

    • Property Getters/Setters Detail

      • getCount

        public int getCount()
        
        Gets the number of elements contained in the collection.

        Example:

        Shows how to map data columns and MERGEFIELDs with different names so the data is transferred between them during a mail merge.
        public void mappedDataFieldCollection() throws Exception
        {
            // Create a document and table that we will merge
            Document doc = createSourceDocMappedDataFields();
            DataTable dataTable = createSourceTableMappedDataFields();
        
            // We have a column "Column2" in the data table that doesn't have a respective MERGEFIELD in the document
            // Also, we have a MERGEFIELD named "Column3" that does not exist as a column in the data source
            // If data from "Column2" is suitable for the "Column3" MERGEFIELD,
            // we can map that column name to the MERGEFIELD in the "MappedDataFields" key/value pair
            MappedDataFieldCollection mappedDataFields = doc.getMailMerge().getMappedDataFields();
        
            // A data source column name is linked to a MERGEFIELD name by adding an element like this
            mappedDataFields.add("MergeFieldName", "DataSourceColumnName");
        
            // So, values from "Column2" will now go into MERGEFIELDs named "Column3" as well as "Column2", if there are any
            mappedDataFields.add("Column3", "Column2");
        
            // The MERGEFIELD name is the "key" to the respective data source column name "value"
            Assert.assertEquals(mappedDataFields.get("MergeFieldName"), "DataSourceColumnName");
            Assert.assertTrue(mappedDataFields.containsKey("MergeFieldName"));
            Assert.assertTrue(mappedDataFields.containsValue("DataSourceColumnName"));
        
            // Now if we run this mail merge, the "Column3" MERGEFIELDs will take data from "Column2" of the table
            doc.getMailMerge().execute(dataTable);
        
            // We can count and iterate over the mapped columns/fields
            Assert.assertEquals(mappedDataFields.getCount(), 2);
        
            Iterator<Map.Entry<String, String>> enumerator = mappedDataFields.iterator();
            try
            {
                while (enumerator.hasNext())
                {
                    Map.Entry<String, String> dataField = enumerator.next();
                    System.out.println(MessageFormat.format("Column named {0} is mapped to MERGEFIELDs named {1}", dataField.getValue(), dataField.getKey()));
                }
            }
            finally { if (enumerator != null) enumerator.remove(); }
        
            // We can also remove some or all of the elements
            mappedDataFields.remove("MergeFieldName");
            Assert.assertFalse(mappedDataFields.containsKey("MergeFieldName"));
            Assert.assertFalse(mappedDataFields.containsValue("DataSourceColumnName"));
        
            mappedDataFields.clear();
            Assert.assertEquals(mappedDataFields.getCount(), 0);
        
            // Removing the mapped key/value pairs has no effect on the document because the merge was already done with them in place
            doc.save(getArtifactsDir() + "MailMerge.MappedDataFieldCollection.docx");
        }
        
        /// <summary>
        /// Create a document with 2 MERGEFIELDs, one of which does not have a corresponding column in the data table.
        /// </summary>
        private static Document createSourceDocMappedDataFields() throws Exception
        {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
        
            // Insert two MERGEFIELDs that will accept data from that table
            builder.insertField(" MERGEFIELD Column1");
            builder.write(", ");
            builder.insertField(" MERGEFIELD Column3");
        
            return doc;
        }
        
        /// <summary>
        /// Create a data table with 2 columns, one of which does not have a corresponding MERGEFIELD in our source document.
        /// </summary>
        private static DataTable createSourceTableMappedDataFields()
        {
            // Create a data table that will be used in a mail merge
            DataTable dataTable = new DataTable("MyTable");
            dataTable.getColumns().add("Column1");
            dataTable.getColumns().add("Column2");
            dataTable.getRows().add(new Object[] { "Value1", "Value2" });
        
            return dataTable;
        }
      • get/set

        public java.lang.String get(java.lang.String documentFieldName) / public void set(java.lang.String documentFieldName, java.lang.String value)
        
        Gets or sets the name of the field in the data source associated with the specified mail merge field.

        Example:

        Shows how to map data columns and MERGEFIELDs with different names so the data is transferred between them during a mail merge.
        public void mappedDataFieldCollection() throws Exception
        {
            // Create a document and table that we will merge
            Document doc = createSourceDocMappedDataFields();
            DataTable dataTable = createSourceTableMappedDataFields();
        
            // We have a column "Column2" in the data table that doesn't have a respective MERGEFIELD in the document
            // Also, we have a MERGEFIELD named "Column3" that does not exist as a column in the data source
            // If data from "Column2" is suitable for the "Column3" MERGEFIELD,
            // we can map that column name to the MERGEFIELD in the "MappedDataFields" key/value pair
            MappedDataFieldCollection mappedDataFields = doc.getMailMerge().getMappedDataFields();
        
            // A data source column name is linked to a MERGEFIELD name by adding an element like this
            mappedDataFields.add("MergeFieldName", "DataSourceColumnName");
        
            // So, values from "Column2" will now go into MERGEFIELDs named "Column3" as well as "Column2", if there are any
            mappedDataFields.add("Column3", "Column2");
        
            // The MERGEFIELD name is the "key" to the respective data source column name "value"
            Assert.assertEquals(mappedDataFields.get("MergeFieldName"), "DataSourceColumnName");
            Assert.assertTrue(mappedDataFields.containsKey("MergeFieldName"));
            Assert.assertTrue(mappedDataFields.containsValue("DataSourceColumnName"));
        
            // Now if we run this mail merge, the "Column3" MERGEFIELDs will take data from "Column2" of the table
            doc.getMailMerge().execute(dataTable);
        
            // We can count and iterate over the mapped columns/fields
            Assert.assertEquals(mappedDataFields.getCount(), 2);
        
            Iterator<Map.Entry<String, String>> enumerator = mappedDataFields.iterator();
            try
            {
                while (enumerator.hasNext())
                {
                    Map.Entry<String, String> dataField = enumerator.next();
                    System.out.println(MessageFormat.format("Column named {0} is mapped to MERGEFIELDs named {1}", dataField.getValue(), dataField.getKey()));
                }
            }
            finally { if (enumerator != null) enumerator.remove(); }
        
            // We can also remove some or all of the elements
            mappedDataFields.remove("MergeFieldName");
            Assert.assertFalse(mappedDataFields.containsKey("MergeFieldName"));
            Assert.assertFalse(mappedDataFields.containsValue("DataSourceColumnName"));
        
            mappedDataFields.clear();
            Assert.assertEquals(mappedDataFields.getCount(), 0);
        
            // Removing the mapped key/value pairs has no effect on the document because the merge was already done with them in place
            doc.save(getArtifactsDir() + "MailMerge.MappedDataFieldCollection.docx");
        }
        
        /// <summary>
        /// Create a document with 2 MERGEFIELDs, one of which does not have a corresponding column in the data table.
        /// </summary>
        private static Document createSourceDocMappedDataFields() throws Exception
        {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
        
            // Insert two MERGEFIELDs that will accept data from that table
            builder.insertField(" MERGEFIELD Column1");
            builder.write(", ");
            builder.insertField(" MERGEFIELD Column3");
        
            return doc;
        }
        
        /// <summary>
        /// Create a data table with 2 columns, one of which does not have a corresponding MERGEFIELD in our source document.
        /// </summary>
        private static DataTable createSourceTableMappedDataFields()
        {
            // Create a data table that will be used in a mail merge
            DataTable dataTable = new DataTable("MyTable");
            dataTable.getColumns().add("Column1");
            dataTable.getColumns().add("Column2");
            dataTable.getRows().add(new Object[] { "Value1", "Value2" });
        
            return dataTable;
        }
    • Method Detail

      • add

        public void add(java.lang.String documentFieldName, java.lang.String dataSourceFieldName)
        Adds a new field mapping.
        Parameters:
        documentFieldName - Case-sensitive name of the mail merge field in the document.
        dataSourceFieldName - Case-sensitive name of the field in the data source.

        Example:

        Shows how to map data columns and MERGEFIELDs with different names so the data is transferred between them during a mail merge.
        public void mappedDataFieldCollection() throws Exception
        {
            // Create a document and table that we will merge
            Document doc = createSourceDocMappedDataFields();
            DataTable dataTable = createSourceTableMappedDataFields();
        
            // We have a column "Column2" in the data table that doesn't have a respective MERGEFIELD in the document
            // Also, we have a MERGEFIELD named "Column3" that does not exist as a column in the data source
            // If data from "Column2" is suitable for the "Column3" MERGEFIELD,
            // we can map that column name to the MERGEFIELD in the "MappedDataFields" key/value pair
            MappedDataFieldCollection mappedDataFields = doc.getMailMerge().getMappedDataFields();
        
            // A data source column name is linked to a MERGEFIELD name by adding an element like this
            mappedDataFields.add("MergeFieldName", "DataSourceColumnName");
        
            // So, values from "Column2" will now go into MERGEFIELDs named "Column3" as well as "Column2", if there are any
            mappedDataFields.add("Column3", "Column2");
        
            // The MERGEFIELD name is the "key" to the respective data source column name "value"
            Assert.assertEquals(mappedDataFields.get("MergeFieldName"), "DataSourceColumnName");
            Assert.assertTrue(mappedDataFields.containsKey("MergeFieldName"));
            Assert.assertTrue(mappedDataFields.containsValue("DataSourceColumnName"));
        
            // Now if we run this mail merge, the "Column3" MERGEFIELDs will take data from "Column2" of the table
            doc.getMailMerge().execute(dataTable);
        
            // We can count and iterate over the mapped columns/fields
            Assert.assertEquals(mappedDataFields.getCount(), 2);
        
            Iterator<Map.Entry<String, String>> enumerator = mappedDataFields.iterator();
            try
            {
                while (enumerator.hasNext())
                {
                    Map.Entry<String, String> dataField = enumerator.next();
                    System.out.println(MessageFormat.format("Column named {0} is mapped to MERGEFIELDs named {1}", dataField.getValue(), dataField.getKey()));
                }
            }
            finally { if (enumerator != null) enumerator.remove(); }
        
            // We can also remove some or all of the elements
            mappedDataFields.remove("MergeFieldName");
            Assert.assertFalse(mappedDataFields.containsKey("MergeFieldName"));
            Assert.assertFalse(mappedDataFields.containsValue("DataSourceColumnName"));
        
            mappedDataFields.clear();
            Assert.assertEquals(mappedDataFields.getCount(), 0);
        
            // Removing the mapped key/value pairs has no effect on the document because the merge was already done with them in place
            doc.save(getArtifactsDir() + "MailMerge.MappedDataFieldCollection.docx");
        }
        
        /// <summary>
        /// Create a document with 2 MERGEFIELDs, one of which does not have a corresponding column in the data table.
        /// </summary>
        private static Document createSourceDocMappedDataFields() throws Exception
        {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
        
            // Insert two MERGEFIELDs that will accept data from that table
            builder.insertField(" MERGEFIELD Column1");
            builder.write(", ");
            builder.insertField(" MERGEFIELD Column3");
        
            return doc;
        }
        
        /// <summary>
        /// Create a data table with 2 columns, one of which does not have a corresponding MERGEFIELD in our source document.
        /// </summary>
        private static DataTable createSourceTableMappedDataFields()
        {
            // Create a data table that will be used in a mail merge
            DataTable dataTable = new DataTable("MyTable");
            dataTable.getColumns().add("Column1");
            dataTable.getColumns().add("Column2");
            dataTable.getRows().add(new Object[] { "Value1", "Value2" });
        
            return dataTable;
        }
      • clear

        public void clear()
        Removes all elements from the collection.

        Example:

        Shows how to map data columns and MERGEFIELDs with different names so the data is transferred between them during a mail merge.
        public void mappedDataFieldCollection() throws Exception
        {
            // Create a document and table that we will merge
            Document doc = createSourceDocMappedDataFields();
            DataTable dataTable = createSourceTableMappedDataFields();
        
            // We have a column "Column2" in the data table that doesn't have a respective MERGEFIELD in the document
            // Also, we have a MERGEFIELD named "Column3" that does not exist as a column in the data source
            // If data from "Column2" is suitable for the "Column3" MERGEFIELD,
            // we can map that column name to the MERGEFIELD in the "MappedDataFields" key/value pair
            MappedDataFieldCollection mappedDataFields = doc.getMailMerge().getMappedDataFields();
        
            // A data source column name is linked to a MERGEFIELD name by adding an element like this
            mappedDataFields.add("MergeFieldName", "DataSourceColumnName");
        
            // So, values from "Column2" will now go into MERGEFIELDs named "Column3" as well as "Column2", if there are any
            mappedDataFields.add("Column3", "Column2");
        
            // The MERGEFIELD name is the "key" to the respective data source column name "value"
            Assert.assertEquals(mappedDataFields.get("MergeFieldName"), "DataSourceColumnName");
            Assert.assertTrue(mappedDataFields.containsKey("MergeFieldName"));
            Assert.assertTrue(mappedDataFields.containsValue("DataSourceColumnName"));
        
            // Now if we run this mail merge, the "Column3" MERGEFIELDs will take data from "Column2" of the table
            doc.getMailMerge().execute(dataTable);
        
            // We can count and iterate over the mapped columns/fields
            Assert.assertEquals(mappedDataFields.getCount(), 2);
        
            Iterator<Map.Entry<String, String>> enumerator = mappedDataFields.iterator();
            try
            {
                while (enumerator.hasNext())
                {
                    Map.Entry<String, String> dataField = enumerator.next();
                    System.out.println(MessageFormat.format("Column named {0} is mapped to MERGEFIELDs named {1}", dataField.getValue(), dataField.getKey()));
                }
            }
            finally { if (enumerator != null) enumerator.remove(); }
        
            // We can also remove some or all of the elements
            mappedDataFields.remove("MergeFieldName");
            Assert.assertFalse(mappedDataFields.containsKey("MergeFieldName"));
            Assert.assertFalse(mappedDataFields.containsValue("DataSourceColumnName"));
        
            mappedDataFields.clear();
            Assert.assertEquals(mappedDataFields.getCount(), 0);
        
            // Removing the mapped key/value pairs has no effect on the document because the merge was already done with them in place
            doc.save(getArtifactsDir() + "MailMerge.MappedDataFieldCollection.docx");
        }
        
        /// <summary>
        /// Create a document with 2 MERGEFIELDs, one of which does not have a corresponding column in the data table.
        /// </summary>
        private static Document createSourceDocMappedDataFields() throws Exception
        {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
        
            // Insert two MERGEFIELDs that will accept data from that table
            builder.insertField(" MERGEFIELD Column1");
            builder.write(", ");
            builder.insertField(" MERGEFIELD Column3");
        
            return doc;
        }
        
        /// <summary>
        /// Create a data table with 2 columns, one of which does not have a corresponding MERGEFIELD in our source document.
        /// </summary>
        private static DataTable createSourceTableMappedDataFields()
        {
            // Create a data table that will be used in a mail merge
            DataTable dataTable = new DataTable("MyTable");
            dataTable.getColumns().add("Column1");
            dataTable.getColumns().add("Column2");
            dataTable.getRows().add(new Object[] { "Value1", "Value2" });
        
            return dataTable;
        }
      • containsKey

        public boolean containsKey(java.lang.String documentFieldName)
        Determines whether a mapping from the specified field in the document exists in the collection.
        Parameters:
        documentFieldName - Case-sensitive name of the mail merge field in the document.
        Returns:
        True if item is found in the collection; otherwise, false.

        Example:

        Shows how to map data columns and MERGEFIELDs with different names so the data is transferred between them during a mail merge.
        public void mappedDataFieldCollection() throws Exception
        {
            // Create a document and table that we will merge
            Document doc = createSourceDocMappedDataFields();
            DataTable dataTable = createSourceTableMappedDataFields();
        
            // We have a column "Column2" in the data table that doesn't have a respective MERGEFIELD in the document
            // Also, we have a MERGEFIELD named "Column3" that does not exist as a column in the data source
            // If data from "Column2" is suitable for the "Column3" MERGEFIELD,
            // we can map that column name to the MERGEFIELD in the "MappedDataFields" key/value pair
            MappedDataFieldCollection mappedDataFields = doc.getMailMerge().getMappedDataFields();
        
            // A data source column name is linked to a MERGEFIELD name by adding an element like this
            mappedDataFields.add("MergeFieldName", "DataSourceColumnName");
        
            // So, values from "Column2" will now go into MERGEFIELDs named "Column3" as well as "Column2", if there are any
            mappedDataFields.add("Column3", "Column2");
        
            // The MERGEFIELD name is the "key" to the respective data source column name "value"
            Assert.assertEquals(mappedDataFields.get("MergeFieldName"), "DataSourceColumnName");
            Assert.assertTrue(mappedDataFields.containsKey("MergeFieldName"));
            Assert.assertTrue(mappedDataFields.containsValue("DataSourceColumnName"));
        
            // Now if we run this mail merge, the "Column3" MERGEFIELDs will take data from "Column2" of the table
            doc.getMailMerge().execute(dataTable);
        
            // We can count and iterate over the mapped columns/fields
            Assert.assertEquals(mappedDataFields.getCount(), 2);
        
            Iterator<Map.Entry<String, String>> enumerator = mappedDataFields.iterator();
            try
            {
                while (enumerator.hasNext())
                {
                    Map.Entry<String, String> dataField = enumerator.next();
                    System.out.println(MessageFormat.format("Column named {0} is mapped to MERGEFIELDs named {1}", dataField.getValue(), dataField.getKey()));
                }
            }
            finally { if (enumerator != null) enumerator.remove(); }
        
            // We can also remove some or all of the elements
            mappedDataFields.remove("MergeFieldName");
            Assert.assertFalse(mappedDataFields.containsKey("MergeFieldName"));
            Assert.assertFalse(mappedDataFields.containsValue("DataSourceColumnName"));
        
            mappedDataFields.clear();
            Assert.assertEquals(mappedDataFields.getCount(), 0);
        
            // Removing the mapped key/value pairs has no effect on the document because the merge was already done with them in place
            doc.save(getArtifactsDir() + "MailMerge.MappedDataFieldCollection.docx");
        }
        
        /// <summary>
        /// Create a document with 2 MERGEFIELDs, one of which does not have a corresponding column in the data table.
        /// </summary>
        private static Document createSourceDocMappedDataFields() throws Exception
        {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
        
            // Insert two MERGEFIELDs that will accept data from that table
            builder.insertField(" MERGEFIELD Column1");
            builder.write(", ");
            builder.insertField(" MERGEFIELD Column3");
        
            return doc;
        }
        
        /// <summary>
        /// Create a data table with 2 columns, one of which does not have a corresponding MERGEFIELD in our source document.
        /// </summary>
        private static DataTable createSourceTableMappedDataFields()
        {
            // Create a data table that will be used in a mail merge
            DataTable dataTable = new DataTable("MyTable");
            dataTable.getColumns().add("Column1");
            dataTable.getColumns().add("Column2");
            dataTable.getRows().add(new Object[] { "Value1", "Value2" });
        
            return dataTable;
        }
      • containsValue

        public boolean containsValue(java.lang.String dataSourceFieldName)
        Determines whether a mapping from the specified field in the data source exists in the collection.
        Parameters:
        dataSourceFieldName - Case-sensitive name of the field in the data source.
        Returns:
        True if item is found in the collection; otherwise, false.

        Example:

        Shows how to map data columns and MERGEFIELDs with different names so the data is transferred between them during a mail merge.
        public void mappedDataFieldCollection() throws Exception
        {
            // Create a document and table that we will merge
            Document doc = createSourceDocMappedDataFields();
            DataTable dataTable = createSourceTableMappedDataFields();
        
            // We have a column "Column2" in the data table that doesn't have a respective MERGEFIELD in the document
            // Also, we have a MERGEFIELD named "Column3" that does not exist as a column in the data source
            // If data from "Column2" is suitable for the "Column3" MERGEFIELD,
            // we can map that column name to the MERGEFIELD in the "MappedDataFields" key/value pair
            MappedDataFieldCollection mappedDataFields = doc.getMailMerge().getMappedDataFields();
        
            // A data source column name is linked to a MERGEFIELD name by adding an element like this
            mappedDataFields.add("MergeFieldName", "DataSourceColumnName");
        
            // So, values from "Column2" will now go into MERGEFIELDs named "Column3" as well as "Column2", if there are any
            mappedDataFields.add("Column3", "Column2");
        
            // The MERGEFIELD name is the "key" to the respective data source column name "value"
            Assert.assertEquals(mappedDataFields.get("MergeFieldName"), "DataSourceColumnName");
            Assert.assertTrue(mappedDataFields.containsKey("MergeFieldName"));
            Assert.assertTrue(mappedDataFields.containsValue("DataSourceColumnName"));
        
            // Now if we run this mail merge, the "Column3" MERGEFIELDs will take data from "Column2" of the table
            doc.getMailMerge().execute(dataTable);
        
            // We can count and iterate over the mapped columns/fields
            Assert.assertEquals(mappedDataFields.getCount(), 2);
        
            Iterator<Map.Entry<String, String>> enumerator = mappedDataFields.iterator();
            try
            {
                while (enumerator.hasNext())
                {
                    Map.Entry<String, String> dataField = enumerator.next();
                    System.out.println(MessageFormat.format("Column named {0} is mapped to MERGEFIELDs named {1}", dataField.getValue(), dataField.getKey()));
                }
            }
            finally { if (enumerator != null) enumerator.remove(); }
        
            // We can also remove some or all of the elements
            mappedDataFields.remove("MergeFieldName");
            Assert.assertFalse(mappedDataFields.containsKey("MergeFieldName"));
            Assert.assertFalse(mappedDataFields.containsValue("DataSourceColumnName"));
        
            mappedDataFields.clear();
            Assert.assertEquals(mappedDataFields.getCount(), 0);
        
            // Removing the mapped key/value pairs has no effect on the document because the merge was already done with them in place
            doc.save(getArtifactsDir() + "MailMerge.MappedDataFieldCollection.docx");
        }
        
        /// <summary>
        /// Create a document with 2 MERGEFIELDs, one of which does not have a corresponding column in the data table.
        /// </summary>
        private static Document createSourceDocMappedDataFields() throws Exception
        {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
        
            // Insert two MERGEFIELDs that will accept data from that table
            builder.insertField(" MERGEFIELD Column1");
            builder.write(", ");
            builder.insertField(" MERGEFIELD Column3");
        
            return doc;
        }
        
        /// <summary>
        /// Create a data table with 2 columns, one of which does not have a corresponding MERGEFIELD in our source document.
        /// </summary>
        private static DataTable createSourceTableMappedDataFields()
        {
            // Create a data table that will be used in a mail merge
            DataTable dataTable = new DataTable("MyTable");
            dataTable.getColumns().add("Column1");
            dataTable.getColumns().add("Column2");
            dataTable.getRows().add(new Object[] { "Value1", "Value2" });
        
            return dataTable;
        }
      • iterator

        public java.util.Iterator<java.util.Map.Entry<java.lang.String, java.lang.String>> iterator()
        Returns a dictionary iterator object that can be used to iterate over all items in the collection.

        Example:

        Shows how to map data columns and MERGEFIELDs with different names so the data is transferred between them during a mail merge.
        public void mappedDataFieldCollection() throws Exception
        {
            // Create a document and table that we will merge
            Document doc = createSourceDocMappedDataFields();
            DataTable dataTable = createSourceTableMappedDataFields();
        
            // We have a column "Column2" in the data table that doesn't have a respective MERGEFIELD in the document
            // Also, we have a MERGEFIELD named "Column3" that does not exist as a column in the data source
            // If data from "Column2" is suitable for the "Column3" MERGEFIELD,
            // we can map that column name to the MERGEFIELD in the "MappedDataFields" key/value pair
            MappedDataFieldCollection mappedDataFields = doc.getMailMerge().getMappedDataFields();
        
            // A data source column name is linked to a MERGEFIELD name by adding an element like this
            mappedDataFields.add("MergeFieldName", "DataSourceColumnName");
        
            // So, values from "Column2" will now go into MERGEFIELDs named "Column3" as well as "Column2", if there are any
            mappedDataFields.add("Column3", "Column2");
        
            // The MERGEFIELD name is the "key" to the respective data source column name "value"
            Assert.assertEquals(mappedDataFields.get("MergeFieldName"), "DataSourceColumnName");
            Assert.assertTrue(mappedDataFields.containsKey("MergeFieldName"));
            Assert.assertTrue(mappedDataFields.containsValue("DataSourceColumnName"));
        
            // Now if we run this mail merge, the "Column3" MERGEFIELDs will take data from "Column2" of the table
            doc.getMailMerge().execute(dataTable);
        
            // We can count and iterate over the mapped columns/fields
            Assert.assertEquals(mappedDataFields.getCount(), 2);
        
            Iterator<Map.Entry<String, String>> enumerator = mappedDataFields.iterator();
            try
            {
                while (enumerator.hasNext())
                {
                    Map.Entry<String, String> dataField = enumerator.next();
                    System.out.println(MessageFormat.format("Column named {0} is mapped to MERGEFIELDs named {1}", dataField.getValue(), dataField.getKey()));
                }
            }
            finally { if (enumerator != null) enumerator.remove(); }
        
            // We can also remove some or all of the elements
            mappedDataFields.remove("MergeFieldName");
            Assert.assertFalse(mappedDataFields.containsKey("MergeFieldName"));
            Assert.assertFalse(mappedDataFields.containsValue("DataSourceColumnName"));
        
            mappedDataFields.clear();
            Assert.assertEquals(mappedDataFields.getCount(), 0);
        
            // Removing the mapped key/value pairs has no effect on the document because the merge was already done with them in place
            doc.save(getArtifactsDir() + "MailMerge.MappedDataFieldCollection.docx");
        }
        
        /// <summary>
        /// Create a document with 2 MERGEFIELDs, one of which does not have a corresponding column in the data table.
        /// </summary>
        private static Document createSourceDocMappedDataFields() throws Exception
        {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
        
            // Insert two MERGEFIELDs that will accept data from that table
            builder.insertField(" MERGEFIELD Column1");
            builder.write(", ");
            builder.insertField(" MERGEFIELD Column3");
        
            return doc;
        }
        
        /// <summary>
        /// Create a data table with 2 columns, one of which does not have a corresponding MERGEFIELD in our source document.
        /// </summary>
        private static DataTable createSourceTableMappedDataFields()
        {
            // Create a data table that will be used in a mail merge
            DataTable dataTable = new DataTable("MyTable");
            dataTable.getColumns().add("Column1");
            dataTable.getColumns().add("Column2");
            dataTable.getRows().add(new Object[] { "Value1", "Value2" });
        
            return dataTable;
        }
      • remove

        public void remove(java.lang.String documentFieldName)
        Removes a field mapping.
        Parameters:
        documentFieldName - Case-sensitive name of the mail merge field in the document.

        Example:

        Shows how to map data columns and MERGEFIELDs with different names so the data is transferred between them during a mail merge.
        public void mappedDataFieldCollection() throws Exception
        {
            // Create a document and table that we will merge
            Document doc = createSourceDocMappedDataFields();
            DataTable dataTable = createSourceTableMappedDataFields();
        
            // We have a column "Column2" in the data table that doesn't have a respective MERGEFIELD in the document
            // Also, we have a MERGEFIELD named "Column3" that does not exist as a column in the data source
            // If data from "Column2" is suitable for the "Column3" MERGEFIELD,
            // we can map that column name to the MERGEFIELD in the "MappedDataFields" key/value pair
            MappedDataFieldCollection mappedDataFields = doc.getMailMerge().getMappedDataFields();
        
            // A data source column name is linked to a MERGEFIELD name by adding an element like this
            mappedDataFields.add("MergeFieldName", "DataSourceColumnName");
        
            // So, values from "Column2" will now go into MERGEFIELDs named "Column3" as well as "Column2", if there are any
            mappedDataFields.add("Column3", "Column2");
        
            // The MERGEFIELD name is the "key" to the respective data source column name "value"
            Assert.assertEquals(mappedDataFields.get("MergeFieldName"), "DataSourceColumnName");
            Assert.assertTrue(mappedDataFields.containsKey("MergeFieldName"));
            Assert.assertTrue(mappedDataFields.containsValue("DataSourceColumnName"));
        
            // Now if we run this mail merge, the "Column3" MERGEFIELDs will take data from "Column2" of the table
            doc.getMailMerge().execute(dataTable);
        
            // We can count and iterate over the mapped columns/fields
            Assert.assertEquals(mappedDataFields.getCount(), 2);
        
            Iterator<Map.Entry<String, String>> enumerator = mappedDataFields.iterator();
            try
            {
                while (enumerator.hasNext())
                {
                    Map.Entry<String, String> dataField = enumerator.next();
                    System.out.println(MessageFormat.format("Column named {0} is mapped to MERGEFIELDs named {1}", dataField.getValue(), dataField.getKey()));
                }
            }
            finally { if (enumerator != null) enumerator.remove(); }
        
            // We can also remove some or all of the elements
            mappedDataFields.remove("MergeFieldName");
            Assert.assertFalse(mappedDataFields.containsKey("MergeFieldName"));
            Assert.assertFalse(mappedDataFields.containsValue("DataSourceColumnName"));
        
            mappedDataFields.clear();
            Assert.assertEquals(mappedDataFields.getCount(), 0);
        
            // Removing the mapped key/value pairs has no effect on the document because the merge was already done with them in place
            doc.save(getArtifactsDir() + "MailMerge.MappedDataFieldCollection.docx");
        }
        
        /// <summary>
        /// Create a document with 2 MERGEFIELDs, one of which does not have a corresponding column in the data table.
        /// </summary>
        private static Document createSourceDocMappedDataFields() throws Exception
        {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
        
            // Insert two MERGEFIELDs that will accept data from that table
            builder.insertField(" MERGEFIELD Column1");
            builder.write(", ");
            builder.insertField(" MERGEFIELD Column3");
        
            return doc;
        }
        
        /// <summary>
        /// Create a data table with 2 columns, one of which does not have a corresponding MERGEFIELD in our source document.
        /// </summary>
        private static DataTable createSourceTableMappedDataFields()
        {
            // Create a data table that will be used in a mail merge
            DataTable dataTable = new DataTable("MyTable");
            dataTable.getColumns().add("Column1");
            dataTable.getColumns().add("Column2");
            dataTable.getRows().add(new Object[] { "Value1", "Value2" });
        
            return dataTable;
        }